这是你需要的吗?
unsigned char pin[6]; // Unsigned ...
pin[0] = 0x61;
pin[1] = 0xC7;
pin[2] = 0x5E;
pin[3] = 0x00;
pin[4] = 0x9E;
pin[5] = 0xCC;
printf("%02x:%02x:%02x:%02x:%02x:%02x\n",pin[0],pin[1],pin[2],pin[3],pin[4],pin[5]);
将打印
61:c7:5e:00:9e:cc
解释:
%x // Print a number in hexadecimal format using lower case (%X for upper case)
%2x // Print at least 2 characters (prepend with space)
%02x // Print at least 2 characters and prepend with 0
用于unsigned char pin[6]
避免打印过程中的符号扩展。如果你使用char pin[6]
你会得到
61:ffffffc7:5e:00:ffffff9e:ffffffcc
这可能不是你想要的。
如果您出于某种原因需要使用char
,您可以执行以下操作:
char pin[6];
pin[0] = 0x61;
pin[1] = 0xC7;
pin[2] = 0x5E;
pin[3] = 0x00;
pin[4] = 0x9E;
pin[5] = 0xCC;
printf("%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx\n",pin[0],pin[1],pin[2],pin[3],pin[4],pin[5]);
^^
Force type to char