C - 十六进制输出中缺少 0

1 c hex byte

我必须测试我的机器是使用小端还是大端。出于这个原因,我写了这段代码。我可以看到我的机器正在使用小端。但我不知道为什么我的第一个字节的输出只有 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)

Rem*_*eau 5

%X不输出前导零。使用%02X来代替。将2它告诉给输出至少2位,并且0它告诉给垫左侧的输出与'0'如果输出小于2个位数字。