将十六进制缓冲区转换为无符号整数
我一直在尝试将缓冲区中保存的十六进制数转换为无符号整数。然而,我正在读取的每个十六进制数字前面的“0x00”一直给我带来问题,本质上问题(在缩小版本中)看起来像这样:
char b[] = "0x0014A12";
std::stringstream ss;
unsigned int i;
ss << std::hex << b;
ss >> i;
cout << i << endl;
有什么提示吗?
注意:程序输出一个高位十进制数字,等于十六进制的 CCCCCC。
I've been trying to convert a hexadecimal number saved in a buffer to an unsigned int. However the "0x00" in front of every hexadecimal number that I'm reading from has been giving me problem, in essence the problem (in a downscaled version) looks like this:
char b[] = "0x0014A12";
std::stringstream ss;
unsigned int i;
ss << std::hex << b;
ss >> i;
cout << i << endl;
Any tips?
Note: The program outputs a high decimal nubmer which equals CCCCCC in hex.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这对我来说效果很好:
输出:
This works fine for me:
output:
以下对我有用:
The following works for me:
对于此类问题,我更喜欢使用 sscanf 。
I prefer sscanf for this kind of problem.