BinaryWriter Endian 问题

编程入门 行业动态 更新时间:2024-10-27 22:22:04
本文介绍了BinaryWriter Endian 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用 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 问题

本文发布于:2023-05-25 00:26:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/218676.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:BinaryWriter   Endian

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!