-4

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?

1 Answers1

1

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    why read the documentation when you can read stackoverflow – Ocanath Sep 30 '20 at 16:14
  • 1
    I realize this was a LONG time ago, but for the cynical among us, let's remember that there are many programmers who a) don't know where to find documentation, b) haven't developed their Googling skills yet, and c) can't immediately process the large strings of words thrown at them which are often found in documentation. Also, documentation often sucks, so many don't consider it the first place to look. There's no need for snarky second lines about "it would have been very obvious." Let's be kind to each other here :) – Dylan Turner Jan 01 '21 at 08:37