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?

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top