在联合结构方面需要帮助。我正在接收由各种数据包组成的字节流,因此我将字节数据放入联合结构并通过结构成员访问所需的数据。问题在于 uint32_t 类型成员 - 读取跳过其两个字节并在通过其成员访问时显示错误值。这是完整的演示代码:
PacketUtils.h
#include <stdint.h>
typedef struct {
uint8_t startSymbol;
uint8_t packetType;
uint32_t deviceId;
uint16_t packetCRC;
} PacketData;
typedef union {
uint8_t *bytes; // stores raw bytes
PacketData *packet;
} Packet;
// Puts bytes into predefined struct
void getPacketFromBytes(void *bytes, Packet *packetRef);
PacketUtils.c
#include <stdio.h>
#include "UnionStruct.h"
void getPacketFromBytes(void *bytes, Packet *packetRef)
{
uint8_t *rawBytes = (uint8_t *)bytes;
packetRef->bytes = rawBytes;
}
调用代码:
// sample byte data
uint8_t packetBytes[] = {0x11, 0x02, 0x01, 0x01, 0x01, 0x03, 0xbb, 0xbd};
Packet packetRef;
getPacketFromBytes(packetBytes, &packetRef);
printf("%x\n", packetRef.packet->startSymbol); // good - prints 0x11
printf("%x\n", packetRef.packet->packetType); // good - prints 0x02
printf("%x\n", packetRef.packet->deviceId); // bad - prints bd bb 03 01
printf("%x\n", packetRef.packet->packetCRC); // bad - prints 36 80 (some next values in memory)
当 PacketData 结构由 uint8_t 或 uint16_t 类型成员组成时一切正常,然后打印显示正确的值。但是,打印 uint32_t 类型的 deviceId 会跳过两个字节 (0x01 0x01) 并抓取最后 4 个字节。打印 packetCRC 打印给定字节数组中的值 - 内存中的一些值,如 packetBytes[12] 和 packetBytes[13]。我不明白为什么它会跳过两个字节......