How to convert hexadecimal number to IP address in C++
When you need to convert hexadecimal number in C++ like 0DF52286 to standard IP address format like 13.245.34.134 you can use this code:
1 2 3 4 5 6 7 8 9 10 |
static char* hex_to_ip(const char *input) { char *output = (char*)malloc(sizeof(char) * 16); unsigned int a, b, c, d; if (sscanf(input, "%2x%2x%2x%2x", &a, &b, &c, &d) != 4) return output; sprintf(output, "%u.%u.%u.%u", a, b, c, d); return output; } |
The method takes one argument which is the IP address in hexadecimal format.