Looking at all the answers, I think that maybe we are missing another way to do this.
const unsigned char chararr[]="abceXYZ"; for (int i=0; i< 7; ++i) { printf("%#04X %d %c\n", chararr[i], chararr[i], chararr[i]); } 0X61 97 a 0X62 98 b 0X63 99 c 0X65 101 e 0X58 88 X 0X59 89 Y 0X5A 90 Z
If you use% # 04x small x, then the output will be b 0x small x prefix. The # pound sign tells the function to print 0x. 04 to indicate how many digits to print, if the input is set to "0x0a", it will print it, and without 04 - "0xa".
On my computer, a Dell workstation, the result is as expected. If not
unsigned char status = 0x00; printf("status = (0x%02X)\n\r", (status |= 0xC0));
It is better to illustrate with examples:
37 printf("status = (%#02x)\n", (status |= 0xC0)); 38 printf("status = (%#04x)\n", (status |= 0xC0)); 39 printf("status = (%#04x)\n", 0x0f); 40 printf("status = (%#02x)\n", 0x0f); status = (0xc0) status = (0xc0) status = (0x0f) status = (0xf)
source share