查看所有答案,我认为我们可能缺少另一种方法。
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
如果你使用 %#04x small x 那么输出将是 b 0x small x 前缀。# 井号告诉函数打印 0x。04 指示要输出多少位,如果输入是'0x0a',它将打印这个,没有04它将打印'0xa'。
在我的电脑戴尔工作站中,输出与问题所预期的一样。除非
unsigned char status = 0x00;
printf("status = (0x%02X)\n\r", (status |= 0xC0));
// output
//status = (0xC0)
// is exactly expected by the original question.
通过示例更好地说明:
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)