c++ 十六进制转字符串(ASCII码)
#include <iostream>
using namespace std;
unsigned int ssstrlen(const char *str)
{
const char *cp = str;
while(*cp++);
return (cp - str -1);
}
int hex2char(uint8_t c)
{
return ((c >= '0') && (c <= '9')) ? int(c - '0') :
((c >= 'A') && (c <= 'F')) ? int(c - 'A' + 10) :
((c >= 'a') && (c <= 'f')) ? int(c - 'a' + 10) :
-1;
}
int Hex2Ascii(char* hex, char* ascii)
{
int hexLen = ssstrlen(hex);
int asciiLen = 0;
for (int i = 0, cnt = 0; i<hexLen; i++)
{
char c = hex2char(hex[i]);
if (-1 == c)
continue;
if(cnt) {
cnt = 0;
ascii[asciiLen++] += c;
} else {
cnt = 1;
ascii[asciiLen] = c << 4;
}
}
ascii[asciiLen++] = 0;
return asciiLen;
}
int main()
{
char in[64] = { " " };
int len = strlen(outbuffer);
int idx = 0;
for (int n = 0; n < len; n++)
{
in[idx++] = outbuffer[n];
if ((n + 1) % 2 == 0)
{
in[idx++] = ' ';//赋一个空格
}
}
char in[] = "43 52 2b 45 56 45 4e 54 3d 54 42 5f 50 41 55 53 45 3b ";
char out[100];
Hex2Ascii(in, out);
int len = strlen(out);
for(int i=0; i<len; i++)
cout << out[i];
return 0;
}
转换输出结果为:CR+EVENT=TB_PAUSE;
转转:https://blog.csdn.net/u013925378/article/details/107632817