Eva*_*ran 217
使用 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/string/basic_string/stol和http://en.cppreference.com/w/cpp/string/ basic_string/stoul).这些基本上是围绕C的字符串到数字转换函数的薄包装器,但是知道如何处理std::string
因此,对于较新的代码,最简单的答案可能如下所示:
std::string s = "0xfffefffe";
unsigned int x = std::stoul(s, nullptr, 16);
注意:以下是我的原始答案,编辑说这不是一个完整的答案.对于功能性解决方案,请将代码粘贴在以下行:-).
似乎自lexical_cast<>定义为具有流转换语义.遗憾的是,流不理解"0x"符号.因此boost::lexical_cast,我和我的手滚动一个不适合十六进制字符串.手动将输入流设置为十六进制的上述解决方案将很好地处理它.
Boost也有一些东西可以做到这一点,它也有一些很好的错误检查功能.你可以像这样使用它:
try {
    unsigned int x = lexical_cast<int>("0x0badc0de");
} catch(bad_lexical_cast &) {
    // whatever you want to do...
}
如果您不想使用boost,这里是一个轻量级的词法转换版本,不会进行错误检查:
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"); 
小智 57
对于适用于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;
    }
}
Mik*_*ndy 26
Andy Buchanan,至于坚持使用C++,我喜欢你的,但我有几个mod:
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");
这样,每个int类型不需要一个impl.
Kir*_*sky 16
工作示例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在转换之前将输入字符串视为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在控制台中打印.
小智 8
这是我在其他地方找到的一种简单而有效的方法:
string hexString = "7FF";
int hexNumber;
sscanf(hexString.c_str(), "%x", &hexNumber);
请注意,您可能更喜欢使用无符号长整数/长整数来接收该值.另请注意,c_str()函数只是将std :: string转换为const char*.
因此,如果您准备好了const char*,请直接使用该变量名,如下所示[我还显示了对于更大的十六进制数的unsigned long变量的用法.不要将它与const char*而不是string]的情况混淆:
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...
(当我寻找一种不那么糟糕的方式时找到这个页面:-)
干杯,A.
只需使用 stoi/stol/stoll 例如:
std::cout << std::stol("fffefffe", nullptr, 16) << std::endl;
输出:4294901758