How to convert a hex string to an unsigned long?

Chris_vr picture Chris_vr · May 9, 2011 · Viewed 15.1k times · Source

I have following hex value

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

How to convert this in to an unsigned long?

Answer

Adam picture Adam · May 9, 2011

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/