Compile using gcc 4.1.2 on CentOS-5 64 bit.
Printing an integer as a two-character hex string:
#include
#include
A int8_t
will go through the usual integer promotions as a parameter to a variadic function like printf()
. This typically means the int8_t
is converted to int
.
Yet "%X"
is meant for unsigned
arguments. So covert to some unsigned type first and use a matching format specifier:
For uint8_t
, use PRIX8
. With exact width types, include
for the matching specifiers.
#include
printf(" int negative var is <%02" PRIX8 ">\n", iNegVar);`
With int8_t iNegVar = -1;
, convert to some unsigned type before printing in hex
printf(" int negative var is <%02" PRIX8 ">\n", (uint8_t) iNegVar);`