简体   繁体   English

使用write方法格式化写入C ++流的数据

[英]Formatting data written to C++ stream using write method

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. 我尝试将流设置为使用十六进制格式标志,但是使用write方法时似乎没有任何效果。 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. hex标志影响整数值的格式。 When you call write with a buffer it's not formatting anything. 当您使用缓冲区调用write ,它不会格式化任何内容。 You need something roughly like this: 您需要大致像这样的东西:

for (size_t ii = 0; ii < dataLength; ++ii) {
    out << int(pointerToData[ii]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM