C++ convert hex string to signed integer

前端 未结 9 1862
耶瑟儿~
耶瑟儿~ 2020-11-22 03:08

I want to convert a hex string to a 32 bit signed integer in C++.

So, for example, I have the hex string \"fffefffe\". The binary representation of this is 111111

相关标签:
9条回答
  • 2020-11-22 03:12

    I had the same problem today, here's how I solved it so I could keep 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...
    

    (Found this page when I was looking for a less sucky way :-)

    Cheers, A.

    0 讨论(0)
  • 2020-11-22 03:14

    This worked for me:

    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 
    
    0 讨论(0)
  • 2020-11-22 03:18

    just use stoi/stol/stoll for example:

    std::cout << std::stol("fffefffe", nullptr, 16) << std::endl;
    

    output: 4294901758

    0 讨论(0)
  • 2020-11-22 03:19

    For a method that works with both C and C++, you might want to consider using the standard library function 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;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 03:20

    use std::stringstream

    unsigned int x;   
    std::stringstream ss;
    ss << std::hex << "fffefffe";
    ss >> x;
    

    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;
    }
    

    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 :-).

    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 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...
    }
    

    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;
    }
    

    which you can use like this:

    // though this needs the 0x prefix so it knows it is hex
    unsigned int x = lexical_cast<unsigned int>("0xdeadbeef"); 
    
    0 讨论(0)
  • 2020-11-22 03:22

    Here's a simple and working method I found elsewhere:

    string hexString = "7FF";
    int hexNumber;
    sscanf(hexString.c_str(), "%x", &hexNumber);
    

    Please note that you might prefer using unsigned long integer/long integer, to receive the value. Another note, the c_str() function just converts the std::string to const char* .

    So if you have a const char* ready, just go ahead with using that variable name directly, as shown below [I am also showing the usage of the unsigned long variable for a larger hex number. Do not confuse it with the case of having const char* instead of 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);
    

    This works just perfectly fine (provided you use appropriate data types per your need).

    0 讨论(0)
提交回复
热议问题