I get a network transmission that is an array of characters / bytes. It contains a header and some data. I would like to match the title with the structure. Here is an example:
#pragma pack(1)
struct Header
{
unsigned short bodyLength;
int msgID;
unsigned short someOtherValue;
unsigned short protocolVersion;
};
int main()
{
boost::array<char, 128> msgBuffer;
Header header;
for(int x = 0; x < sizeof(Header); x++)
msgBuffer[x] = 0x01;
memcpy(&header, msgBuffer.data(), sizeof(Header));
system("PAUSE");
return 0;
}
Will this always work if the structure never contains variable length fields? Is there an independent / idiomatic way of the platform?
Note:
I have seen quite a few libraries on the Internet that allow you to serialize / deserialize, but I get the impression that they can only deserialize something if it was previously serialized with the same library. Well, I do not control the transmission format. I am definitely going to get a byte / char array where all the values just follow each other.