Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BinaryWriter Endian issue

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);

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?

like image 327
Icemanind Avatar asked Oct 08 '09 20:10

Icemanind


2 Answers

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(...);

It doesn't derive from BinaryWriter, for reasons given in a blog post.

like image 116
Jon Skeet Avatar answered Oct 21 '22 01:10

Jon Skeet


according to microsoft connect, it's currently not supported: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=484149

like image 20
Michael Abdelmalek Avatar answered Oct 21 '22 02:10

Michael Abdelmalek