You have already used sprintf to represent decimal values as a string; you can do the same for hexadecimal formatting.
Use the return value of sprintf() (which is the number of characters 'printed') to advance an index into the buffer as the destination for the next hex-digit pair, then iterate each character in the decimal buffer.
Note that, you can use the return value (m) from the first sprintf() for the iteration rather then testing for nul.
char hexbuffer[64];
int index = 0 ;
for(int i = 0; i < m; i++)
{
index += sprintf( &hexbuffer[index], "%02X", buffer[i] ) ;
}
Of course if the input includes only decimal digits (i.e. the user does not enter a negative value causing a - at buffer[0], you could simply insert a 3 ahead of each decimal digit, thus:
char hexbuffer[64];
for(int i = 0; i < m; i++)
{
bexbuffer[i * 2] = '3' ;
hexbuffer[i * 2 + 1] = buffer[i]
}
If only decimal digits were intended, you should change the type of a to unsigned and use the %u format specifier for the decimal input.
123, if you're not getting313233for output, what are you getting instead? I just downloaded and tested your code and it works, so double confused :-)printfprints into a buffer, you need a second buffer (e.g.char hexbuffer[50]). Now, replace yourprintfwith:sprintf(&hexbuffer[i * 2],"%02X",buffer[i]). You can't do it in a single buffer because you're outputting twice as many charschar buffer[20];? Note: hadabeen 64-bit, 21 may be needed forsprintf(buffer, "%d", a);.