Domanda

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);
È stato utile?

Soluzione

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]);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top