I need to decode a message from a std::string
object.
I can see how to do this in Java by using a ByteBuffer, but I failed to find a simple C++ equivalent.
More precisely, how to implement this?
const std::string serialized_data = ...; // Source
SomeDataType data = ???; // How to convert from serialized_data to data?
You can write code like this:
Note that this assumes that the backing buffer of an
std::string
is always word-aligned. I think this is true, but I am not sure. If it is not aligned, you will get exceptions saying so. In this case it may be necessary to copy the data into an aligned buffer:Note that while
std::string
is technically able to store arbitrary bytes, it is not a particularly good data structure for this purpose, since it is optimized for text. For example, it may re-allocate and copy your buffer in order to add a NUL terminator to support.c_str()
. I would recommend using a more data-oriented type for byte arrays.