Search code examples
cprintfpicpic18

Force printf to use two characters


I'm using this code on a PIC18 device with the XC8 compiler:

printf("%x", (unsigned char) some_value);

When some_value is below 0xf0, only one digit is outputted, e.g. c for the value 12. On values above 0x0f, two digits are outputted, e.g. 42 for the value 66.

Is there a way to force printf() to output two characters, also on values below 0xf0?


Solution

  • Of course:

    printf("%02x", ...);
    

    It would have been very obvious if you had only read any (and I really mean any) documentation about printf and its formatting.