Of course:
int array[4] = {1, 2, 3, 4}; char* c = reinterpret_cast<char*>(array);
The valid range is c to c + sizeof(array) . You can do this with any type of POD.
You can drop from a sequence of bytes:
// assuming c above int (&pArray)[4] = *reinterpret_cast<int(*)[4]>(c);
This is guaranteed. But it looks like you are trying to send stuff over the network, which may lead to other problems.
The process you are looking for is called serialization (and has a FAQ) . This is when you take an object, convert it to a series of bits, which can subsequently be "deserialized" to the original object.
Doing this work on multiple platforms can be difficult because you need to make sure that you serialize to a specific format and that each platform knows how to read it from that format. (For example, the big-endian platform can always be converted to little-endian before being sent and likewise converted back to big-endian upon receipt.) You cannot treat non-POD types as a stream of bytes (for example, std::string ), therefore you need to write serialization functions for those to convert their data to a stream of bytes and deserialization functions to convert them back.
I especially like that Boost does this, and if you can use their serialization library . They basically first define procedures for serializing fundamental types, then you can serialize more complex types by building them. Of course, Boost also has an ASIO library for creating sockets for you.
source share