Convert Hex string to unsigned long

Unsigned hexadecimal string I have a hexadecimal value
CString str; str = T ("FFF000");

How to convert this value to unsigned long?

+3
source share
2 answers
#include <sstream>
#include <iostream>

int main()
{

    std::string s("0xFFF000");
    unsigned long value;
    std::istringstream iss(s);
    iss >> std::hex >> value;
    std::cout << value << std::endl;

    return 0;
}
+7
source

You can use a function strtolthat works with regular C strings. It converts the string to a long one using the specified base:

long l = strtol(str, NULL, 16);

details and a good example: http://www.cplusplus.com/reference/clibrary/cstdlib/strtol/

+11
source

All Articles