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]);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top