简体   繁体   English

如何将十六进制字符串转换为unsigned long?

[英]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 ? 如何将此转换为unsigned long

You can use strtol function which works on regular C strings. 您可以使用strtol函数,该函数适用于常规C字符串。 It converts a string to a long using a specified base: 它使用指定的基础将字符串转换为long:

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

details and good example: http://www.cplusplus.com/reference/clibrary/cstdlib/strtol/ 详细信息和良好示例: 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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