Visual Studio C++ 2008 操纵字节?
我正在尝试将严格的二进制数据写入文件(无编码)。问题是,当我十六进制转储文件时,我注意到相当奇怪的行为。使用以下任一方法构建文件都会产生相同的行为。我什至使用 System::Text::Encoding::Default
来测试流。
StreamWriter^ binWriter = gcnew StreamWriter(gcnew FileStream("test.bin",FileMode::Create));
(Also used this method)
FileStream^ tempBin = gcnew FileStream("test.bin",FileMode::Create);
BinaryWriter^ binWriter = gcnew BinaryWriter(tempBin);
binWriter->Write(0x80);
binWriter->Write(0x81);
.
.
binWriter->Write(0x8F);
binWriter->Write(0x90);
binWriter->Write(0x91);
.
.
binWriter->Write(0x9F);
写入该字节序列时,我注意到在十六进制转储中未转换为0x3F
的唯一字节是0x81
、0x8D
,0x90
,0x9D
,...我不知道为什么。
我也尝试过制作字符数组,也出现了类似的情况。即,
array<wchar_t,1>^ OT_Random_Delta_Limits = {0x00,0x00,0x03,0x79,0x00,0x00,0x04,0x88};
binWriter->Write(OT_Random_Delta_Limits);
0x88
将被写为0x3F
。
I'm trying to write strictly binary data to files (no encoding). The problem is, when I hex dump the files, I'm noticing rather weird behavior. Using either one of the below methods to construct a file results in the same behavior. I even used the System::Text::Encoding::Default
to test as well for the streams.
StreamWriter^ binWriter = gcnew StreamWriter(gcnew FileStream("test.bin",FileMode::Create));
(Also used this method)
FileStream^ tempBin = gcnew FileStream("test.bin",FileMode::Create);
BinaryWriter^ binWriter = gcnew BinaryWriter(tempBin);
binWriter->Write(0x80);
binWriter->Write(0x81);
.
.
binWriter->Write(0x8F);
binWriter->Write(0x90);
binWriter->Write(0x91);
.
.
binWriter->Write(0x9F);
Writing that sequence of bytes, I noticed the only bytes that weren't converted to 0x3F
in the hex dump were 0x81
,0x8D
,0x90
,0x9D
, ... and I have no idea why.
I also tried making character arrays, and a similar situation happens. i.e.,
array<wchar_t,1>^ OT_Random_Delta_Limits = {0x00,0x00,0x03,0x79,0x00,0x00,0x04,0x88};
binWriter->Write(OT_Random_Delta_Limits);
0x88
would be written as 0x3F
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想坚持使用二进制文件,那么不要使用
StreamWriter
。只需使用FileStream
和Write
/WriteByte
即可。 StreamWriter(以及一般的 TextWriter)是专门为文本设计的。无论您是否需要编码,都会应用一种编码 - 因为当您调用StreamWriter.Write
时,写入的是char
,而不是byte
代码>.也不要创建
wchar_t
值数组 - 同样,这些值用于字符,即文本。BinaryWriter.Write
应该对您有用,除非它将值提升为char
在这种情况下您会遇到完全相同的问题。顺便说一句,如果不指定任何编码,我希望您获得非 0x3F 值,而是代表这些字符的 UTF-8 编码值的字节。
当您指定
Encoding.Default
时,您会看到任何不属于该编码的 Unicode 值都是 0x3F。不管怎样,当你想处理二进制数据而不是文本时,基本的教训是坚持使用
Stream
。编辑:好的,它会是这样的:
这在缓冲等方面非常低效,但应该可以让您开始。
If you want to stick to binary files then don't use
StreamWriter
. Just use aFileStream
andWrite
/WriteByte
. StreamWriters (and TextWriters in generally) are expressly designed for text. Whether you want an encoding or not, one will be applied - because when you're callingStreamWriter.Write
, that's writing achar
, not abyte
.Don't create arrays of
wchar_t
values either - again, those are for characters, i.e. text.BinaryWriter.Write
should have worked for you unless it was promoting the values tochar
in which case you'd have exactly the same problem.By the way, without specifying any encoding, I'd expect you to get non-0x3F values, but instead the bytes representing the UTF-8 encoded values for those characters.
When you specified
Encoding.Default
, you'd have seen 0x3F for any Unicode values not in that encoding.Anyway, the basic lesson is to stick to
Stream
when you want to deal with binary data rather than text.EDIT: Okay, it would be something like:
This is very inefficient in terms of buffering etc, but should get you started.
使用流初始化的
BinaryWriter()
类将对写入的任何字符或字符串使用默认编码 UTF8。我猜测这些调用绑定到
Write( char)
重载,因此它们将通过字符编码器。我对 C++/CLI 不太熟悉,但在我看来,这些调用应该绑定到Write(Int32)
,这不应该有这个问题(也许你的代码真的在调用 < code>Write() 并使用设置为示例中的值的char
变量来解释此行为)。A
BinaryWriter()
class initialized with a stream will use a default encoding of UTF8 for any chars or strings that are written. I'm guessing that thecalls are binding to the
Write( char)
overload so they're going through the character encoder. I'm not very familiar with C++/CLI, but it seems to me that these calls should be binding toWrite(Int32)
, which shouldn't have this problem (maybe your code is really callingWrite()
with achar
variable that's set to the values in your example. That would account for this behavior).0x3F 通常称为 ASCII 字符“?”;映射到它的字符是没有可打印表示的控制字符。正如 Jon 指出的,对于原始二进制数据使用二进制流而不是面向文本的输出机制。
编辑——实际上你的结果看起来与我的预期相反。在默认的代码页 1252 中,不可打印的字符(即可能映射的字符)到“?”)在该范围内是 0x81、0x8D、0x8F、0x90 和 0x9D
0x3F is commonly known as the ASCII character '?'; the characters that are mapping to it are control characters with no printable representation. As Jon points out, use a binary stream rather than a text-oriented output mechanism for raw binary data.
EDIT -- actually your results look like the inverse of what I would expect. In the default code page 1252, the non-printable characters (i.e. ones likely to map to '?') in that range are 0x81, 0x8D, 0x8F, 0x90 and 0x9D