I have unsigned char[] that contains binary text. I need to convert this binary text into hexadecimal. I have managed to do just this and output the result to a file. However, for speed reasons, I would like to directly output this to a char[] so I can further manipulate it. The following is the code that writes out to the file:
for( i = 0; i < rsa.len; i++ )
fprintf( f, "%02X%s", buf[i],
( i + 1 ) % 16 == 0 ? "\r\n" : " " );
And buf is defined as follows:
unsigned char buf[512];
The output to the file looks like:
11 51 64 36 7A 9D 6C E8 F5 5C B2 29 2D 2F 1B 87
0A 20 23 3F B4 B0 41 5F A1 5F 54 6A C4 44 49 4B
58 C1 91 67 7D 7F 70 8D 20 9A 86 06 89 3A A8 2A
26 18 7A CE AB C0 7B 2A D7 A4 B2 5B C6 76 EB EA
90 F9 59 6C 78 4A 7C B2 A6 AE 46 3E E0 A7 A7 6A
F6 81 E3 70 78 B7 0D CC 8B D2 2C 23 42 EB 3B 90
88 15 AC 4C 84 FD 24 40 4F 08 C8 36 89 04 E4 5E
F1 AF CF FE 68 75 38 9C 75 0A 22 C2 44 49 35 A2
If it matters buf is the binary output of an 512 bit RSA encryption.
I was thinking of using sprintf() to print to a char[] in hexadecimal but I can’t seem to get it to work correctly.
The best-performant solution would be to write HEX yourself. This is not a difficult task if you use a lookup table: