I have a struct
typedef struct {
uint8_t type; // 1B -> 1B
uint16_t hash; // 2B -> 3B
uint16_t id; // 2B -> 5B
uint32_t ip; // 4B -> 9B
uint16_t port; // 2B -> 11B
} Data;
and some binary data (which is a stored instance of Data on disk)
const unsigned char blob[11] = { 0x00, 0x00, 0x7b, 0x00, 0xea, 0x00, 0x00, 0x00, 0x59, 0x01, 0x00 };
I want to "read" the blob into my struct, the first byte 0x00 corresponds to type, the second and third byte 0x00, 0x7b correspond to hash, etc.
I can't just do Data *data = (Data *)blob, since the actual size of Data will probably be bigger than 11 Bytes (Faster RAM access or something. Not relevant here.) The point is sizeof(Data) == 16 and the representation in RAM may be different than the compact one on disk.
So how can I "import" my blob into a Data struct without having to use memcpy for every attribute? Aka what's nicest/simplest solution for this in C?
unionfor the overlapping fields (other thantype).#pragma pack?