c + +十六进制字符串转换为有符号整数
题
欲十六进制字符串转换为32位带符号整数在C ++中。
因此,例如,我有十六进制字符串“fffefffe”。这样做的二进制表示11111111111111101111111111111110.这样做的有符号整数表示为:-65538。
如何做在C ++这种转换?这也需要非负数工作。例如,十六进制字符串“0000000A”,这是00000000000000000000000000001010二进制,和10(十进制)。
解决方案
使用std::stringstream
unsigned int x;
std::stringstream ss;
ss << std::hex << "fffefffe";
ss >> x;
下面的例子中产生-65538
作为其结果是:
#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 /串/ basic_string的/ STOL 和 http://en.cppreference.com/w/cpp/string/basic_string/stoul )。这些是围绕C'S字符串数转换功能基本上薄包装纸,但是知道如何处理一个std::string
因此,对于新的代码最简单的答案可能会是这样的:
std::string s = "0xfffefffe";
unsigned int x = std::stoul(s, nullptr, 16);
注意:下面是我原来的答复,这是编辑说的是不是一个完整的答案。对于一个功能的解决方案,粘线之上的代码: - 。)
显然由于lexical_cast<>
被定义为具有流转换语义。可悲的是,流不懂“0x”为符号。所以无论是boost::lexical_cast
和我的手挽一个不与十六进制字符串处理好。上述溶液中,其中输入流手动设置为十六进制将处理它就好了。
升压有一些东西来做到这一点如好了,其中有一些不错的错误检查功能以及。您可以使用它是这样的:
try {
unsigned int x = lexical_cast<int>("0x0badc0de");
} catch(bad_lexical_cast &) {
// whatever you want to do...
}
如果你不喜欢使用升压,这里有一个简易版本词汇投这没有错误检查:
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 ++工程的方法,你可能要考虑使用标准库函数strtol()。
#include <cstdlib>
#include <iostream>
using namespace std;
int main() {
string s = "abcd";
char * p;
long n = strtol( s.c_str(), & p, 16 );
if ( * p != 0 ) { //my bad edit was here
cout << "not a number" << endl;
}
else {
cout << n << endl;
}
}
安迪布坎南,就粘到C ++推移,我喜欢你的,但是我有几个MODS的:
template <typename ElemT>
struct HexTo {
ElemT value;
operator ElemT() const {return value;}
friend std::istream& operator>>(std::istream& in, HexTo& out) {
in >> std::hex >> out.value;
return in;
}
};
用于像
uint32_t value = boost::lexical_cast<HexTo<uint32_t> >("0x2a");
这样就不需要每个整型一个IMPL。
工作与strtoul
示例将是:
#include <cstdlib>
#include <iostream>
using namespace std;
int main() {
string s = "fffefffe";
char * p;
long n = strtoul( s.c_str(), & p, 16 );
if ( * p != 0 ) {
cout << "not a number" << endl;
} else {
cout << n << endl;
}
}
strtol
转换string
到long
。在我的电脑numeric_limits<long>::max()
给0x7fffffff
。显然,这0xfffefffe
比0x7fffffff
更大。所以strtol
回报MAX_LONG
,而不是需要的值。 strtoul
转换string
到unsigned long
这就是为什么在这种情况下没有溢出。
确定,strtol
正在考虑convertation之前输入的字符串不是作为32位有符号整数。与strtol
滑稽样品:
#include <cstdlib>
#include <iostream>
using namespace std;
int main() {
string s = "-0x10002";
char * p;
long n = strtol( s.c_str(), & p, 16 );
if ( * p != 0 ) {
cout << "not a number" << endl;
} else {
cout << n << endl;
}
}
上面打印的代码在-65538
控制台。
下面是一个简单的和工作方法我在别处找到:
string hexString = "7FF";
int hexNumber;
sscanf(hexString.c_str(), "%x", &hexNumber);
请注意,您可能更喜欢使用无符号长整型/长整型,无法接收该值。 另一说明,c_str()函数只转换的std :: string为const char *。
所以,如果你有一个const char *准备好了,只是直接使用该变量名,如下图所示[我也呈现出了较大的十六进制数的无符号长变量的使用继续。不具有常量字符的情况下混淆的*代替字符串]:
const char *hexString = "7FFEA5"; //Just to show the conversion of a bigger hex number
unsigned long hexNumber; //In case your hex number is going to be sufficiently big.
sscanf(hexString, "%x", &hexNumber);
这工作得非常细(只要你使用每您的需要适当的数据类型)。
今天我有同样的问题,这里是我如何解决它,所以我可以不断的lexical_cast <>
typedef unsigned int uint32;
typedef signed int int32;
class uint32_from_hex // For use with boost::lexical_cast
{
uint32 value;
public:
operator uint32() const { return value; }
friend std::istream& operator>>( std::istream& in, uint32_from_hex& outValue )
{
in >> std::hex >> outValue.value;
}
};
class int32_from_hex // For use with boost::lexical_cast
{
uint32 value;
public:
operator int32() const { return static_cast<int32>( value ); }
friend std::istream& operator>>( std::istream& in, int32_from_hex& outValue )
{
in >> std::hex >> outvalue.value;
}
};
uint32 material0 = lexical_cast<uint32_from_hex>( "0x4ad" );
uint32 material1 = lexical_cast<uint32_from_hex>( "4ad" );
uint32 material2 = lexical_cast<uint32>( "1197" );
int32 materialX = lexical_cast<int32_from_hex>( "0xfffefffe" );
int32 materialY = lexical_cast<int32_from_hex>( "fffefffe" );
// etc...
(实测当前页时,我正在寻找一种较少苏茨基方式: - )
干杯, 甲
这为我工作:
string string_test = "80123456";
unsigned long x;
signed long val;
std::stringstream ss;
ss << std::hex << string_test;
ss >> x;
// ss >> val; // if I try this val = 0
val = (signed long)x; // However, if I cast the unsigned result I get val = 0x80123456
另一种方法
using namespace System;
template <typename NUM>
NUM hexstr2num(String^ h)
{
NUM v=0;
String^ k=L"0123456789ABCDEF";
short l=h->Length;
char off;
h=h->ToUpper();
if(h->Substring(0,1)!=L"H")
{if(h->Substring(0,2)==L"0X") off=2;}
else
{off=1;}
if(!off) throw;
char dx=0;
for(int i=l;i>off;i--)
{
if((dx=k->IndexOf(h->Substring(i-1,1)))>=0)
{v+=dx<<((l-i)<<2);}
else
{throw;}
}
return v;
}