的BinaryWriter尾段问题尾段、问题、BinaryWriter

2023-09-02 10:23:41 作者:神乐

我使用的BinaryWriter类写一个二进制文件保存到磁盘。当我调用Write方法,传递一个无符号短值,它写入在小端格式。例如:

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使用大端?如果不是,是有可能创造一个新的类,继承的BinaryWriter和过载的写入功能,使其写大端?

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?

推荐答案

您可以用我的 EndianBinaryWriter 中的 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.