char 数组的二进制表示

Binary representation of a char array

我有一个并集,就是uint8的8个字段,int32的2个字段,或者int64的一个。

我试图在其中保存一个包含 8 个值的数组:0, 0, 0, 1, 0, 0, 0, 1

不幸的是,我正在尝试解决的软件中有一个错误,我不明白这个数组的二进制表示形式。

我的工会:

union {
    uint8_t u08[8];
    uint32_t u32[2];
    uint64_t u64;
} data;

我的调试器输出:

Name : u08
Details:"[=11=][=11=][=11=][=11=]1[=11=][=11=][=11=][=11=]1"
Default:0x4001e6a0 <ucHeap+66948>
Decimal:1073866400
Hex:0x4001e6a0
Binary:1000000000000011110011010100000
Octal:010000363240

Name : u32
Details:{1, 1}
Default:0x4001e6a0 <ucHeap+66948>
Decimal:1073866400
Hex:0x4001e6a0
Binary:1000000000000011110011010100000
Octal:010000363240

Name : u64
Details:4294967297
Default:4294967297
Decimal:4294967297
Hex:0x100000001
Binary:100000000000000000000000000000001
Octal:040000000001

我的问题是:

  1. 为什么 u64 的值与其他值不同?据我了解,c只为联合的一个变量保存space来存储值。
  2. uint8 数组的二进制表示是什么意思?即使它是 ASCII(它不是,因为我发送的是直接值,而不是字符),那么 '0' 是 110000 而 '1' 是 110001

我正在使用 NXP 的 MPC5748G 和 S32DS(基于 eclipse IDE)进行调试。

你所看到的并不是你所认为的。那里的一切都是正确的。 u08 & u32 是数组。 u64是一个整数值,调试器显示不同的信息

Name : u08
Details:"[=10=][=10=][=10=][=10=]1[=10=][=10=][=10=][=10=]1"    -- actual values stored in the array
Default:0x4001e6a0 <ucHeap+66948> -- address of the array u08
Decimal:1073866400                -- same address in different bases
Hex:0x4001e6a0
Binary:1000000000000011110011010100000
Octal:010000363240

Name : u32
Details:{1, 1}                     -- actual values stored in the array
Default:0x4001e6a0 <ucHeap+66948>  -- address of the array u32
Decimal:1073866400                 -- same address in different bases
Hex:0x4001e6a0
Binary:1000000000000011110011010100000
Octal:010000363240

Name : u64
Details:4294967297                 -- value stored un the u64 integer
Default:4294967297                 -- same value in different bases
Decimal:4294967297
Hex:0x100000001
Binary:100000000000000000000000000000001
Octal:040000000001

{0, 0, 0, 1, 0, 0, 0, 1} 是大端表示法:

  • 0x00000001 (1) 在 u32
  • 0x00000001000000010x100000001 没有零)在 u64

如果您想查看所有成员的相同信息,您需要将最后一个成员设为数组:

union {
    uint8_t u08[8];
    uint32_t u32[2];
    uint64_t u64[1];
} data;