writing ints to file in reverse byte order

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I need to support a file format that stores integers with the MSB first
and the LSB last. When I use
BinaryWriter aWriter = new BinaryWriter(aFileStream, Encoding.ASCII);
int someValue = 1;
aWriter.WriteInt32(someValue);
the byte stream that appears in the file is
01 00 00 00

What I need for this file format is
00 00 00 01

There does not appear to be anything in Marshal that supports this. Does
anyone have a recommendation?

Thanks in advance,
 
Hello,

I need to support a file format that stores integers with the MSB first
and the LSB last. When I use
BinaryWriter aWriter = new BinaryWriter(aFileStream, Encoding.ASCII);
int someValue = 1;
aWriter.WriteInt32(someValue);
the byte stream that appears in the file is
01 00 00 00

What I need for this file format is
00 00 00 01

There does not appear to be anything in Marshal that supports this. Does
anyone have a recommendation?

Thanks in advance,

You could try using System.Net.IPAddress.HostToNetworkOrder to convert
the integer first....

using System;
using System.Net;

public class test
{
public static void Main ()
{
int i = 1;
int j = IPAddress.HostToNetworkOrder (i);

Console.WriteLine (BitConverter.ToString (BitConverter.GetBytes (i)));
Console.WriteLine (BitConverter.ToString (BitConverter.GetBytes (j)));
}
}
 
You can also do by yourself with some bit shifting:

public void WriteInt32BigEndian ( int value )
{
base.Write( new byte [0x4] {
(byte) (value >> 0x18),
(byte) (value >> 0x10),
(byte) (value >> 0x8),
(byte) (value)
} );
}
 
PHil Coveney said:
I need to support a file format that stores integers with the MSB first
and the LSB last. When I use
BinaryWriter aWriter = new BinaryWriter(aFileStream, Encoding.ASCII);
int someValue = 1;
aWriter.WriteInt32(someValue);
the byte stream that appears in the file is
01 00 00 00

What I need for this file format is
00 00 00 01

There does not appear to be anything in Marshal that supports this. Does
anyone have a recommendation?

You could use my EndianBinaryWriter which allows you to specify
endianness:

http://www.pobox.com/~skeet/csharp/miscutil
 
Hello,

I need to support a file format that stores integers with the MSB first
and the LSB last. When I use
BinaryWriter aWriter = new BinaryWriter(aFileStream, Encoding.ASCII);
int someValue = 1;
aWriter.WriteInt32(someValue);
the byte stream that appears in the file is
01 00 00 00

What I need for this file format is
00 00 00 01

There does not appear to be anything in Marshal that supports this. Does
anyone have a recommendation?

Thanks in advance,

In addition, check out the system.Text.Encoding.BigEndianUnicode encoding
method.

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 
Hi Jon,

Wow, I got several very helpful responses to this post...but yours, the
one that said "use this, it's already done," wins the prize. Thanks for your
help, and your DLL.

PC
 
PHil Coveney said:
Wow, I got several very helpful responses to this post...but yours, the
one that said "use this, it's already done," wins the prize. Thanks for your
help, and your DLL.

My pleasure - just let me know if you have any problems with it :)
 
Back
Top