The hex
flag affects how integer values are formatted. When you call write
with a buffer it's not formatting anything. You need something roughly like this:
for (size_t ii = 0; ii < dataLength; ++ii) {
out << int(pointerToData[ii]);
}
Вопрос
I'm trying to write the contents of a pointer to a stream in hex. I've tried setting the stream to use the hex format flag, but it doesn't seem to have an effect when using the write method. Below is my current code:
char* pointerToData;
size_t dataLength;
....
out.setf(std::ios::hex, std::ios::basefield);
out.setf(std::ios::showbase);
out.write(pointerToData, dataLength);
os.unsetf(std::ios::showbase);
Решение
The hex
flag affects how integer values are formatted. When you call write
with a buffer it's not formatting anything. You need something roughly like this:
for (size_t ii = 0; ii < dataLength; ++ii) {
out << int(pointerToData[ii]);
}