C++ convert hex string to signed integer(C ++将十六进制字符串转换为有符号整数)
问题描述
我想在 C++ 中将十六进制字符串转换为 32 位有符号整数.
I want to convert a hex string to a 32 bit signed integer in C++.
因此,例如,我有十六进制字符串ffffeffe".它的二进制表示是 11111111111111101111111111111110.它的有符号整数表示是:-65538.
So, for example, I have the hex string "fffefffe". The binary representation of this is 11111111111111101111111111111110. The signed integer representation of this is: -65538.
如何在 C++ 中进行这种转换?这也需要对非负数起作用.比如十六进制字符串0000000A",二进制是00000000000000000000000000001010,十进制是10.
How do I do this conversion in C++? This also needs to work for non-negative numbers. For example, the hex string "0000000A", which is 00000000000000000000000000001010 in binary, and 10 in decimal.
推荐答案
使用std::stringstream
unsigned int x;
std::stringstream ss;
ss << std::hex << "fffefffe";
ss >> x;
以下示例生成 -65538
作为其结果:
the following example produces -65538
as its result:
#include <sstream>
#include <iostream>
int main() {
unsigned int x;
std::stringstream ss;
ss << std::hex << "fffefffe";
ss >> x;
// output it as a signed type
std::cout << static_cast<int>(x) << std::endl;
}
在新的 C++11 标准中,有一些新的实用函数可供您使用!具体来说,有一系列字符串到数字"函数(http://en.cppreference.com/w/cpp/string/basic_string/stol 和 http://en.cppreference.com/w/cpp/string/basic_string/stoul).这些本质上是围绕 C 的字符串到数字转换函数的薄包装,但知道如何处理 std::string
In the new C++11 standard, there are a few new utility functions which you can make use of! specifically, there is a family of "string to number" functions (http://en.cppreference.com/w/cpp/string/basic_string/stol and http://en.cppreference.com/w/cpp/string/basic_string/stoul). These are essentially thin wrappers around C's string to number conversion functions, but know how to deal with a std::string
因此,较新代码的最简单答案可能如下所示:
So, the simplest answer for newer code would probably look like this:
std::string s = "0xfffefffe";
unsigned int x = std::stoul(s, nullptr, 16);
<小时>
注意:以下是我的原始答案,正如编辑所说,这不是一个完整的答案.对于功能性解决方案,请将代码粘贴在行上方:-).
NOTE: Below is my original answer, which as the edit says is not a complete answer. For a functional solution, stick the code above the line :-).
看来,由于 lexical_cast<>
被定义为具有流转换语义.可悲的是,流不理解0x"表示法.所以 boost::lexical_cast
和我的手卷都不能很好地处理十六进制字符串.上述手动将输入流设置为十六进制的解决方案可以很好地处理它.
It appears that since lexical_cast<>
is defined to have stream conversion semantics. Sadly, streams don't understand the "0x" notation. So both the boost::lexical_cast
and my hand rolled one don't deal well with hex strings. The above solution which manually sets the input stream to hex will handle it just fine.
Boost 有一些东西可以做到这一点好吧,它也有一些很好的错误检查功能.你可以这样使用它:
Boost has some stuff to do this as well, which has some nice error checking capabilities as well. You can use it like this:
try {
unsigned int x = lexical_cast<int>("0x0badc0de");
} catch(bad_lexical_cast &) {
// whatever you want to do...
}
如果您不想使用 boost,这里有一个不进行错误检查的轻量级词法转换:
If you don't feel like using boost, here's a light version of lexical cast which does no error checking:
template<typename T2, typename T1>
inline T2 lexical_cast(const T1 &in) {
T2 out;
std::stringstream ss;
ss << in;
ss >> out;
return out;
}
你可以这样使用:
// though this needs the 0x prefix so it knows it is hex
unsigned int x = lexical_cast<unsigned int>("0xdeadbeef");
这篇关于C ++将十六进制字符串转换为有符号整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C ++将十六进制字符串转换为有符号整数


基础教程推荐
- 从 std::cin 读取密码 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07