C++ convert hex string to signed integer

前端 未结 9 1986
耶瑟儿~
耶瑟儿~ 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: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 
    

提交回复
热议问题