Is there any char to hexadecimal function for C?

c, hex

Solution

If I understand the question correctly (no guarantees there), you have a text string representing a number in decimal format ("1234"), and you want to convert it to a string in hexadecimal format ("4d2").

Assuming that's correct, your best bet will be to convert the input string to an integer using either `sscanf()` or `strtol()`, then use `sprintf()` with the `%x` conversion specifier to write the hex version to another string:

char text[] = "1234";
char result[SIZE]; // where SIZE is big enough to hold any converted value
int val;

val = (int) strtol(text, NULL, 0); // error checking omitted for brevity
sprintf(result, "%x", val);

Problem

I have a char array with data from a text file and I need to convert it to hexadecimal format. Is there such a function for C language. Thank you in advance!

Original source