4

I have following hex value

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

How to convert this in to an unsigned long?

iammilind
  • 68,093
  • 33
  • 169
  • 336
Vikram Ranabhatt
  • 7,268
  • 15
  • 70
  • 133

2 Answers2

12

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/

Adam
  • 16,808
  • 7
  • 52
  • 98
11
#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;
}
hidayat
  • 9,493
  • 13
  • 51
  • 66