#include <stdio.h>
#include <stdint.h>
struct stRequestMsg {
uint16_t startTag;
uint32_t messageSize;
uint16_t messageID;
uint16_t sequenceNumber;
uint16_t messageType;
uint32_t reserved;
};
void printIt(struct stRequestMsg m) {
" .startTag = %#x;\n"
" .messageSize = %#x;\n"
" .messageID = %#x;\n"
" .sequenceNumber = %#x;\n"
" .messageType = %#x;\n"
" .reserved = %#x;\n"
"}\n",
m.startTag, m.messageSize, m.messageID,
m.sequenceNumber, m.messageType, m.reserved);
}
uint16_t deserialize_uint16(char const * const b) {
return ((uint16_t) b[0] << 8u) |
((uint16_t) b[1]);
}
uint32_t deserialize_uint32(char const * const b) {
return ((uint16_t) b[0] << 24u) |
((uint16_t) b[1] << 16u) |
((uint16_t) b[2] << 8u) |
((uint16_t) b[3]);
}
struct stRequestMsg deserialize(char const * const b) {
struct stRequestMsg r;
r.startTag = deserialize_uint16(b);
r.messageSize = deserialize_uint32(b + 2);
r.messageID = deserialize_uint16(b + 6);
r.sequenceNumber = deserialize_uint16(b + 8);
r.messageType = deserialize_uint16(b + 10);
r.reserved = deserialize_uint32(b + 12);
return r;
}
int main(void) {
char buff[16];
buff[0] = 0x50;
buff[1] = 0x00;
buff[2] = 0x1E;
buff[3] = 0x00;
buff[4] = 0x00;
buff[5] = 0x00;
buff[6] = 0x31;
buff[7] = 0x00;
buff[8] = 0x10;
buff[9] = 0x00;
buff[10] = 0x20;
buff[11] = 0x00;
buff[12] = 0x10;
buff[13] = 0x00;
buff[14] = 0x00;
buff[15] = 0x00;
struct stRequestMsg msg = deserialize(&(buff[0]));
printIt(msg);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRpbnQuaD4KCnN0cnVjdCBzdFJlcXVlc3RNc2cgewogICAgdWludDE2X3Qgc3RhcnRUYWc7CiAgICB1aW50MzJfdCBtZXNzYWdlU2l6ZTsKICAgIHVpbnQxNl90IG1lc3NhZ2VJRDsKICAgIHVpbnQxNl90IHNlcXVlbmNlT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