最近在项目中调试串口,,总结封装了字符串转化为16进制显示的算法,串口数据发送一般为ASCII和16进制两种,当收到数据时数据也有ASCII和16进制显示两种方式,下面给出一种转化算法,该算法定义实现两种版本函数:ASCII和UNICODE. 由于算法使用C++标准库的string作为返回字符串,因此是比较通用的.
算法原理是对字符串内存进行操作转化,以一个字节(unsigned char类型)为单位,分别取其高4位和低4位(范围为0x0--0xF), 转化为对应的ASCII字符('0'--'F')显示,代码如下
1
/**
2 @brief StrToHex ASCII版本
3 @param lpSrc 源字符串
4 @param chTag 显示分隔符,默认为0表示空字符
5 @return 返回转化后16进制字符串
6*/
7 static string StringToHex(LPCSTR lpSrc, char chTag = 0)
8 {
9 string strDest;
10 unsigned char* pSrc = (unsigned char*)lpSrc;
11 char buf[2];
12
13 long dwSize = strlen(lpSrc);
14 for (long dwIndex = 0; dwIndex < dwSize; ++dwIndex)
15 {
16 unsigned char c0 = *pSrc >> 4;
17 if ( c0 >= 0x0 && c0 <= 0x9)
18 {
19 buf[0] = c0 - 0 + '0';
20 }
21 else
22 {
23 buf[0] = c0 - 10 + 'A';
24 }
25 unsigned char c1 = *pSrc++ & 0x0F;
26 if ( c1 >= 0x0 && c1 <= 0x9)
27 {
28 buf[1] = c1 - 0 + '0';
29 }
30 else
31 {
32 buf[1] = c1 - 10 + 'A';
33 }
34 strDest += buf[0];
35 strDest += buf[1];
36 if (0 != chTag) strDest += chTag;
37 }
38 return strDest;
39}
40 /**
41 @brief StrToHex UNICODE版本
42 @param lpSrc 源字符串
43 @param chTag 显示分隔符,默认为0表示空字符
44 @return 返回转化后16进制字符串
45*/
46 static wstring StringToHex(LPCWSTR lpSrc, wchar_t chTag = 0)
47 {
48 wstring strDest;
49 unsigned char* pSrc = (unsigned char*)lpSrc;
50 char buf[2];
51
52 long dwSize = wcslen(lpSrc) * sizeof(wchar_t);
53 for (long dwIndex = 0; dwIndex < dwSize; ++dwIndex)
54 {
55 unsigned char c0 = *pSrc >> 4;
56 if ( c0 >= 0x0 && c0 <= 0x9)
57 {
58 buf[0] = c0 - 0 + '0';
59 }
60 else
61 {
62 buf[0] = c0 - 10 + 'A';
63 }
64 unsigned char c1 = *pSrc++ & 0x0F;
65 if ( c1 >= 0x0 && c1 <= 0x9)
66 {
67 buf[1] = c1 - 0 + '0';
68 }
69 else
70 {
71 buf[1] = c1 - 10 + 'A';
72 }
73 strDest += (wchar_t)buf[0];
74 strDest += (wchar_t)buf[1];
75 if (0 != chTag) strDest += chTag;
76 }
77 return strDest;
78}
79 /**
80 @def StrToHex
81 * 字符串转化为16进制显示
82*/
83 #define StrToHex StringToHex
2 @brief StrToHex ASCII版本
3 @param lpSrc 源字符串
4 @param chTag 显示分隔符,默认为0表示空字符
5 @return 返回转化后16进制字符串
6*/
7 static string StringToHex(LPCSTR lpSrc, char chTag = 0)
8 {
9 string strDest;
10 unsigned char* pSrc = (unsigned char*)lpSrc;
11 char buf[2];
12
13 long dwSize = strlen(lpSrc);
14 for (long dwIndex = 0; dwIndex < dwSize; ++dwIndex)
15 {
16 unsigned char c0 = *pSrc >> 4;
17 if ( c0 >= 0x0 && c0 <= 0x9)
18 {
19 buf[0] = c0 - 0 + '0';
20 }
21 else
22 {
23 buf[0] = c0 - 10 + 'A';
24 }
25 unsigned char c1 = *pSrc++ & 0x0F;
26 if ( c1 >= 0x0 && c1 <= 0x9)
27 {
28 buf[1] = c1 - 0 + '0';
29 }
30 else
31 {
32 buf[1] = c1 - 10 + 'A';
33 }
34 strDest += buf[0];
35 strDest += buf[1];
36 if (0 != chTag) strDest += chTag;
37 }
38 return strDest;
39}
40 /**
41 @brief StrToHex UNICODE版本
42 @param lpSrc 源字符串
43 @param chTag 显示分隔符,默认为0表示空字符
44 @return 返回转化后16进制字符串
45*/
46 static wstring StringToHex(LPCWSTR lpSrc, wchar_t chTag = 0)
47 {
48 wstring strDest;
49 unsigned char* pSrc = (unsigned char*)lpSrc;
50 char buf[2];
51
52 long dwSize = wcslen(lpSrc) * sizeof(wchar_t);
53 for (long dwIndex = 0; dwIndex < dwSize; ++dwIndex)
54 {
55 unsigned char c0 = *pSrc >> 4;
56 if ( c0 >= 0x0 && c0 <= 0x9)
57 {
58 buf[0] = c0 - 0 + '0';
59 }
60 else
61 {
62 buf[0] = c0 - 10 + 'A';
63 }
64 unsigned char c1 = *pSrc++ & 0x0F;
65 if ( c1 >= 0x0 && c1 <= 0x9)
66 {
67 buf[1] = c1 - 0 + '0';
68 }
69 else
70 {
71 buf[1] = c1 - 10 + 'A';
72 }
73 strDest += (wchar_t)buf[0];
74 strDest += (wchar_t)buf[1];
75 if (0 != chTag) strDest += chTag;
76 }
77 return strDest;
78}
79 /**
80 @def StrToHex
81 * 字符串转化为16进制显示
82*/
83 #define StrToHex StringToHex
在应用中需要转化时, 只需调用StrToHex宏,示例如下:
1
string
strDest1
=
StrToHex(
"
123456789汉字ABCXYZ
"
);
2 string strDest2 = StrToHex( " 123456789汉字ABCXYZ " , ' ' );
3 wstring wstrDest1 = StrToHex(L " 123456789汉字ABCXYZ " );
4 wstring wstrDest2 = StrToHex(L " 123456789汉字ABCXYZ " , L ' , ' );
5
6 TRACE4( " %s \n " , strDest1.c_str());
7 TRACE4( " %s \n " , strDest2.c_str());
8 TRACE4(L " %s \n " , wstrDest1.c_str());
9 TRACE4(L " %s \n " , wstrDest2.c_str());
2 string strDest2 = StrToHex( " 123456789汉字ABCXYZ " , ' ' );
3 wstring wstrDest1 = StrToHex(L " 123456789汉字ABCXYZ " );
4 wstring wstrDest2 = StrToHex(L " 123456789汉字ABCXYZ " , L ' , ' );
5
6 TRACE4( " %s \n " , strDest1.c_str());
7 TRACE4( " %s \n " , strDest2.c_str());
8 TRACE4(L " %s \n " , wstrDest1.c_str());
9 TRACE4(L " %s \n " , wstrDest2.c_str());
结果输出如下:
1
313233343536373839BABAD7D641424358595A
2 31 32 33 34 35 36 37 38 39 BA BA D7 D6 41 42 43 58 59 5A
3 310032003300340035003600370038003900496C575B410042004300580059005A00
4 31 , 00 , 32 , 00 , 33 , 00 , 34 , 00 , 35 , 00 , 36 , 00 , 37 , 00 , 38 , 00 , 39 , 00 , 49 ,6C, 57 ,5B, 41 , 00 , 42 , 00 , 43 , 00 , 58 , 00 , 59 , 00 ,5A, 00 ,
2 31 32 33 34 35 36 37 38 39 BA BA D7 D6 41 42 43 58 59 5A
3 310032003300340035003600370038003900496C575B410042004300580059005A00
4 31 , 00 , 32 , 00 , 33 , 00 , 34 , 00 , 35 , 00 , 36 , 00 , 37 , 00 , 38 , 00 , 39 , 00 , 49 ,6C, 57 ,5B, 41 , 00 , 42 , 00 , 43 , 00 , 58 , 00 , 59 , 00 ,5A, 00 ,
转自: http://www.cppblog.com/qinqing1984/archive/2009/06/27/88636.html