我正在尝试将无符号字符写入字符串流。
我需要写入的信息包含0x00。我需要将值0到40写为实际的数值,而不是ASCII字符。
编辑:为了澄清,我写的不仅仅是0-40的值。它需要是二进制的。这些进入的东西需要保持原样,而不是一旦写入流就变成字符……
下面是我正在做的事情的要点。
TCHAR _buffer[2]; // This is part of the problem, I figure, since its a char
_buffer[0] = 0x00;
_buffer[1] = 0x01;
tstringstream s;
s.write(_buffer, sizeof(_buffer));
最终发生的是0x00导致字符串流结束,而0x01似乎永远不会到达那里。
我正在以类似的方式阅读:
stream.readsome(_buffer, sizeof(_buffer));
它似乎不想玩好游戏,因为0x00被写入,导致整个事情就这样结束了。
事情就是这样吗,还是我漏掉了什么?我尝试过使用ios_base::binary,也尝试过使用uint8_t而不是TCHAR,但这似乎造成了一团糟的类型转换之类的问题。我担心这可能是必需的路线,但我想在开始之前确定一下。
长话短说,我正在尝试寻找一个与C#的BinaryReader/Writer等价的C++。
谢谢!
发布于 2012-11-02 03:26:17
下面是在VS2012上编译的代码:
#include <iostream>
#include <sstream>
#include <string>
int main()
{
wchar_t buf[] = { 0x0, 0x1, 'a', 'b', 'c' };
std::wstring s(buf);
std::wstringstream ss;
ss.write(buf, 5);
const std::wstring& chars = ss.str();
for (std::wstring::const_iterator it = chars.begin(); it != chars.end(); ++it)
std::cout << std::hex << *it << '\n';
}
给出
0 1 61 62 63按任意键继续。。。
我想这就是你想要的。
https://stackoverflow.com/questions/13183326
复制相似问题