Consider the following snippet which gets some binary data and writes it to an ostringstream
object:
unsigned char* payload;
unsigned long size;
GetData(&payload, &size);
std::cout << md5(payload, size) << std::endl;
std::ostringstream stream;
stream.write((const char*)payload, size);
std::cout << md5(payload, size) << std::endl;
The problem is that, two printed hash values are different form each other, which means payload
has been changed. I tried opening stream
in binary mode by using std::ostringstream stream(std::ios::out | std::ios::binary)
, it did not make a difference, I didn't expect that it would, anyway.
Another fact is, I get a different checksum from the second print statement every time I re-run the program. First hash is always the same.
Now, how can I write my binary data correctly to ostringstream? Can the problem be the cast to const char*
(GetData
method takes an unsigned char**
as the first parameter)?
UPDATE: In the light of comments, here are some more explanations:
- Comparing the binary diff of the original data and the data written, I saw written data has shifted to the right (24 bytes) in some places. It has also some added bytes in the very beggining. I'm still thinking it has something to do with the cast.
- There is no more code between
GetData
and the actual writing. - GetData works correctly, since the checksum after calling it is correct (I know what the checksum should be).
- I cannot post compilable code, because of
GetData
. And it is not necessary, I have isolated the problem to the line wherewrite
is called. - System details are: gcc version 4.6.3 on Ubuntu 12.04 64bit