我必须测试我的机器是使用小端还是大端。出于这个原因,我写了这段代码。我可以看到我的机器正在使用小端。但我不知道为什么我的第一个字节的输出只有 D。它不应该是 0D 吗?
union int2byte
{
unsigned char bytes[4];
unsigned int hex;
};
int main(int argc, const char* argv[])
{
union int2byte hexby;
hexby.hex = 0xBAADF00D;
printf("%u\n",hexby.hex);
int counter;
for(counter = 0; counter < 4; counter = counter + 1)
{
printf("Hex for Byte %u is %X.\n",counter+1, hexby.bytes[counter]);
}
}
Output:
3131961357
Hex for Byte 1 is D.
Hex for Byte 2 is F0.
Hex for Byte 3 is AD.
Hex for Byte 4 is BA.
Run Code Online (Sandbox Code Playgroud)