// Below is a demo program:
#include <stdio.h>
static unsigned char buf0[10], buf1[10], buf2[10];
void main(void){ unsigned char x, c = 0x2d; unsigned int i = 0x0012; unsigned int j = 0x3456; unsigned char b[3] = { 0x78, 0x9a, 0xbc }; while (1) {
x = sprintf(buf0, "0x%02x ", c); // "0x2d00“ x = sprintf(buf1, "0x%04x ", c); // ”0x2d00" x = sprintf(buf2, "0x%02x ", (unsigned int)c); // Cast to integer type, OK x = sprintf(buf2, "0x%04x ", (unsigned int)c); // OK x = sprintf(buf1, "0x%02x ", i); // Integer type, OK x = sprintf(buf1, "0x%02x ", j); // OK
x = sprintf(buf2, "0x%02x ", (unsigned short) b[0]); // Cast to integer type, OK x = sprintf(buf2, "0x%04x ", (unsigned short) b[0]); // OK x = sprintf(buf2, "0x%04x ", (unsigned short) b[1]); // OK x = sprintf(buf2, "0x%02x ", (unsigned short) b[2]); // OK };}