C - Convert long int to signed hex string

Cheetah picture Cheetah · Apr 29, 2010 · Viewed 14.9k times · Source

MASSIVE EDIT:

I have a long int variable that I need to convert to a signed 24bit hexadecimal string without the "0x" at the start. The string must be 6 characters followed by a string terminator '\0', so leading zeros need to be added.

Examples: [-1 -> FFFFFF] --- [1 -> 000001] --- [71 -> 000047]

Answer This seems to do the trick:

long int number = 37;
char string[7];

snprintf (string, 7, "%lX", number);

Answer

user97370 picture user97370 · Apr 29, 2010

Look at sprintf. The %lx specifier does what you want.