简体   繁体   中英

How to convert a hex string to an unsigned long?

I have following hex value

CString str;
str = T("FFF000");

How to convert this in to an unsigned long ?

You can use strtol function which works on regular C strings. It converts a string to a long using a specified base:

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM