As mentioned in other posts, senderId and sequenceNumber are int types that are likely to be larger than char, so these values ​​will be truncated.
If this is acceptable, then the code is fine. If not, then you need to break them into their component bytes. Given that the protocol you use will determine the byte order of multibyte fields, the most portable and least ambiguous way to do this is by switching.
For example, let's say that senderId and sequenceNumber are 2 bytes long, and the protocol requires the high byte to be first:
char* Serialize() { char *message = new char[MaxMailSize]; message[0] = senderId >> 8; message[1] = senderId; message[2] = sequenceNumber >> 8; message[3] = sequenceNumber; memcpy(&message[4], data, MaxDataSize); return message; }
I would also recommend replacing the for loop with memcpy (if available), as it is unlikely to be less efficient, and this will make the code shorter.
Finally, this all assumes that char is one byte long. If this is not so, then all data should be masked, for example:
message[0] = (senderId >> 8) & 0xFF;
source share