Compile using gcc 4.1.2 on CentOS-5 64 bit.
Printing an integer as a two-character hex string:
#include
#include
When you pass char value to sprintf() or any other variadic C function it is promoted to int value -1 in this case. Now you print it as unsigned int by "%X" spec, so you get long number with FF. unsigned char on another side promoted to unsigned int so you get 0xFF integer value which is printed as 2 symbols.
Note: the fact that you specify %X - unsigned int in printf() formatting only affects how printf function treats the value passed to it. Char -> int promotion happens before printf() called and format specifier does not have effect, only type of the value you are passing.