I want to test serialized data conversion in my application, currently the object is stored in file and read the binary file and reloading the object.
In my unit test case I want to test this operation. As the file operations are costly I want to hard code the binary file content in the code itself.
How can I do this?
Currently I am trying like this,
std::string FileContent = "\00\00\00\00\00.........";
and it is not working.
You're right that a string can contain '\0'
, but here you're still initializing it from const char*
, which, by definition, stops at the first '\0'
. I'd recommend you to use uint8_t[]
or even uint32_t[]
(that is, without passing to std::string
), even if the second might have up to 3 bytes of overhead (but it's more compact when in source). That's e.g. how X bitmaps are usually stored.
Another possibility is base64 encoding, which is printable but needs (a relatively quick) decoding.
If you really want to put the const char[]
to a std::string
, first convert the pointer to const char*
, then use the two-iterator constructor of std::string
. While it's true that std::string
can hold '\0'
, it's somewhat an antipattern to store binary in a string, thus I'm not giving the exact code, just the hint.