以十六进制格式转换字符串 [英] Convert String in Hex format
本文介绍了以十六进制格式转换字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的基于MFC Dialogue的应用程序中,有一种情况是用户在编辑控件中输入字符串并希望将其转换为十六进制格式并写入文件。怎么做?
eg1234 5647 4864 4879 1145 --->输入字符串
此字符串转换为十六进制格式!
In my MFC Dialogue based application, there is one situation where user enters string in edit control and want to convert it into hex format & write to file. how to do that?
e.g.1234 5647 4864 4879 1145--->input string
This string is convert into hex format!
推荐答案
for (int i = 0; i < lengthofstring; ++i)
{
int nibble = string[i] >> 4;
printf("%X", nibble);
nibble = string[i] & 0x0F;
printf("%X", nibble);
}
更正的格式字符串。[/ edit]。
[edit]corrected format strings.[/edit].
使用atoi()
,strtol()
或strtoul()
将字符串转换为整数。要创建另一个显示十六进制数字的字符串,请参阅 printf格式规范字段 [ ^ 。这些也适用于CString :: Format()
函数。
示例:
Useatoi()
,strtol()
, orstrtoul()
to convert the string to an integer. To create another string showing the number in hex, see the printf format specification fields[^]. These apply also to theCString::Format()
function.
Example:
LPCTSTR lpszNumber = _T("1234");
int nNumber = _tstoi(lpszNumber);
CString strHex;
strHex.Format(_T("%#X"), nNumber);
十六进制字符串将为''0X4D2''。要使输出没有''0X''前缀,请省略''#''格式标志。
The hex string will be ''0X4D2''. To have the output without the ''0X'' prefix, omit the ''#'' format flag.
这篇关于以十六进制格式转换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文