C - convert long int to signed hexadecimal string

MASSIVE EDIT:

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

Examples: [-1 β†’ FFFFFF] --- [1 β†’ 000001] --- [71 β†’ 000047]

Answer This looks like a trick:

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

snprintf (string, 7, "%lX", number);
+5
source share
4 answers

Since you only need six digits, you may have to disguise yourself to make sure the number is what you need. Something like that:

sprintf(buffer, "%06lx", (unsigned long)val & 0xFFFFFFUL);

, . , (, -2 ^ 23 < x < 2 ^ 23 - 1)

+8

sprintf. %lx , .

+11

itoa. .

, . sprintf, .

+1

, , . , - , , -

sprintf(buffer, "%06X", (int)value & 0xffffff);
+1

All Articles