问题描述
我正在使用 BinaryWriter 类将二进制文件写入磁盘.当我调用 Write 方法,传递一个无符号的 short 值时,它以小端格式写入它.例如:
I am using BinaryWriter class to write a binary file to disk. When I invoke the Write method, passing an unsigned short value, it writes it in little-endian format. For example:
bw.Write(0xA000);
将二进制文件中的值写入为 0x00 0xA0.有没有办法让 BInaryWriter 使用 Big Endian?如果不是,是否可以创建一个新类,继承BinaryWriter并重载Write函数使其写入大端?
writes the value in the binary file as 0x00 0xA0. Is there a way to make BInaryWriter use Big Endian? If not, is it possible to create a new class, inheriting BinaryWriter and overload the Write function to make it write big endian?
推荐答案
你可以在 EndianBinaryWriterrel="noreferrer">MiscUtil.这使您可以指定所需的字节顺序.还有 EndianBinaryReader
和 EndianBitConverter
.
You can use my EndianBinaryWriter
in MiscUtil. That lets you specify the endianness you want. There's also EndianBinaryReader
and EndianBitConverter
.
EndianBinaryWriter writer = new EndianBinaryWriter(EndianBitConverter.Big,
stream);
writer.Write(...);
它不是从 BinaryWriter
派生的,原因是 在博文中给出.
It doesn't derive from BinaryWriter
, for reasons given in a blog post.
这篇关于BinaryWriter Endian 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!