Programming Tips - C/C++: Convert a string holding a hex number to an int

Date: 2012jun18 Update: 2026sep23 Language: C/C++ Keywords: hexadecimal Q. C/C++: Convert a string holding a hex number to an int A. This is how you do it:
int number = strtol(str, NULL, 16); // The "16" means hexadecimal
Full Example:
#include <stdio.h> #include <stdlib.h> // for strtol() int main() { int number = strtol("FF", NULL, 16); // The "16" means hexadecimal printf("number=%d decimal\n", number); printf("number=%x hex\n", number); }
Output:
number=255 decimal number=ff hex