How to convert Hex to Ascii in C with and without sprintf?

I used strtolto convert the string to hex, now I need to print it on the screen. I'm not sure if I can use it sprintf, since I only have 20 thousand. Memory to use on this board. Alternatives are welcome.

+3
source share
3 answers

To do this manually, the easiest way is to display nybbles tables in ASCII:

static const char nybble_chars[] = "0123456789ABCDEF";

Then convert the byte to 2 hexadecimal characters - if it unsigned charis the best representation for the byte, but does not make any assumptions about its size - extract each nybble and get a char for it:

void get_byte_hex( unsigned char b, char *ch1, char *ch2 ) {
    *ch1 = nybble_chars[ ( b >> 4 ) & 0x0F ];
    *ch2 = nybble_chars[ b & 0x0F ];
}

.

+9

, C , .

+1

Test your C library for ltoainversion strtol. Or, write it from scratch (see brone's answer).

0
source

Source: https://habr.com/ru/post/1742710/


All Articles