Initialize union with array?
An old trick is to cast a pointer to an array of bytes to an pointer of a struct (POD) with the exact memory layout of the final message(s).

struct Message {
    int header;
    int payload;
};

// parsing bytes -&gt; messages
char buffer[SIZE];
recv(buffer, 0, SIZE); // replace with actual code for receiving
Message *messages = reinterpret_cast&lt;Message *&gt;(buffer);
size_t max_message_count = SIZE * sizeof(char) / sizeof(Message);


// converting messages -&gt; byte array
Message messages[MSG_COUNT];
// initialize messages
char *bytes = reinterpret_cast&lt;char *&gt;(messages);
size_t byte_count = MSG_COUNT * sizeof(Message) / sizeof(char);


However, this requires a strict memory mapping between the message struct and the serialized byte format (so Message in this case has to be a POD struct). Yes, this effectively bypasses the type system, but if used properly its the fastest way of parsing messages (and in case of embedded systems the reuse of the memory is a nice touch).