最近在项目中调试串口,,总结封装了字符串转化为16进制显示的算法,串口数据发送一般为ASCII和16进制两种,当收到数据时数据也有ASCII和16进制显示两种方式,下面给出一种转化算法,该算法基于字符类型参数化的模板实现,字符串的转化只是调用其内存版本,算法原理是对字符串内存进行操作转化,以一个字节(unsigned char类型)为单位,分别取其高4位和低4位(范围为0x0--0xF), 转化为对应的目标字符('0'--'F')显示,代码如下
1![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//**
2
@brief MemToHex
3
@param template charT 字符类型
4
@param src 源缓冲区
5
@param size lpSrc指向数据的大小,字节数
6
@param tag 显示分隔符,默认为0表示空字符
7
@return 返回转化后16进制字符串
8
*/
9
template<typename charT>
10
inline std::basic_string<charT> MemToHex(const void* src, size_t size, bool upper = true,charT tag = 0)
11![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
12
std::basic_string<charT> strDest;
13
strDest.reserve(2*size);
14
15
unsigned char* pSrc = (unsigned char*)src;
16
unsigned char buf[2];
17![](/Images/OutliningIndicators/InBlock.gif)
18
for (size_t i = 0; i < size; ++i)
19![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
20
unsigned char c0 = *pSrc >> 4;
21
if ( c0 >= 0x0 && c0 <= 0x9)
22
buf[0] = c0 - 0 + '0';
23
else
24
buf[0] = c0 - 10 + (upper ? 'A' : 'a');
25
26
unsigned char c1 = *pSrc++ & 0x0F;
27
if ( c1 >= 0x0 && c1 <= 0x9)
28
buf[1] = c1 - 0 + '0';
29
else
30
buf[1] = c1 - 10 + (upper ? 'A' : 'a');
31
32
strDest += (charT)buf[0];
33
strDest += (charT)buf[1];
34
if (tag != 0) strDest += tag;
35
}
36
return strDest;
37
}
38![](/Images/OutliningIndicators/None.gif)
39![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//**
40
@brief StrToHex
41
@param template d_charT 目标字符类型
42
@param template s_charT 源字符类型
43
@param src 源字符串
44
@param upper true表示大写,false表示小写
45
@param tag 显示分隔符,默认为0表示空字符
46
@return 返回转化后16进制字符串
47
*/
48
template<typename d_charT,typename s_charT>
49
inline std::basic_string<d_charT> StrToHex(const s_charT* src, bool upper = true,d_charT tag = 0)
50![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
51
return MemToHex(src,select_variable<is_ansi_char<s_charT>::value>(strlen,wcslen)(src)*sizeof(s_charT),upper,tag);
52
} 在应用中需要转化时, 只需调用StrToHex函数,示例如下:
1
string strDest1 = StrToHex<char>("123456789汉字ABCXYZ");
2
wstring wstrDest1 = StrToHex<wchar_t>("123456789汉字ABCXYZ",true,' ');
3
string strDest2 = StrToHex<char>(L"123456789汉字ABCXYZ");
4
wstring wstrDest2 = StrToHex<wchar_t>(L"123456789汉字ABCXYZ", true,L',');
5![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
6
TRACE4("%s \n", strDest1.c_str());
7
TRACE4(L"%s \n", wstrDest1.c_str());
8
TRACE4("%s \n", strDest2.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,
posted on 2009-06-27 13:08
春秋十二月 阅读(12778)
评论(6) 编辑 收藏 引用 所属分类:
Algorithm