Byte Order Woes

  • Thread starter Thread starter joey.powell
  • Start date Start date
J

joey.powell

Hello,

I need to be able to read a binary file that contains fields written in
BOTH big endian and little endian byte orders. I am currently unable to
read the big endian fields (erroneous values because of the different
byte orders).

I am currently using FileStream and BinaryReader objects to open and
read the file. As I mentioned above calls to ReadInt32() return
erroneous values becuase of the byte order on the big endian fields.

Is there some sort of "magic" converter, like...

BigEndianToLittleEndian(r.ReadInt32());

that I can use to correctly get at the data?

What is the best method to use to crack this problem?

Thanks in advance.
 
Hello, (e-mail address removed)!

jp> I need to be able to read a binary file that contains fields written in
jp> BOTH big endian and little endian byte orders. I am currently unable to
jp> read the big endian fields (erroneous values because of the different
jp> byte orders).

jp> I am currently using FileStream and BinaryReader objects to open and
jp> read the file. As I mentioned above calls to ReadInt32() return
jp> erroneous values becuase of the byte order on the big endian fields.

jp> Is there some sort of "magic" converter, like...

jp> BigEndianToLittleEndian(r.ReadInt32());

Yep, there is :8-)
Look at
IPAddress.HostToNetworkOrder and vice versa IPAddress.NetworkToHostOrder

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
Hello,

I need to be able to read a binary file that contains fields written in
BOTH big endian and little endian byte orders. I am currently unable to
read the big endian fields (erroneous values because of the different
byte orders).

I am currently using FileStream and BinaryReader objects to open and
read the file. As I mentioned above calls to ReadInt32() return
erroneous values becuase of the byte order on the big endian fields.

Is there some sort of "magic" converter, like...

BigEndianToLittleEndian(r.ReadInt32());

that I can use to correctly get at the data?

What is the best method to use to crack this problem?

Thanks in advance.

Hi Joey,

Try this out for size:

///
public int SwitchEndian ( int i )
{
return ( (i & 0xFF000000) >> 24 ) |
( (i & 0xFF0000) >> 8 ) |
( (i & 0xFF00) << 8 ) |
( (i & 0xFF) << 24 );
}
///

-- Tom Spink
 
Hello,

I need to be able to read a binary file that contains fields written in
BOTH big endian and little endian byte orders. I am currently unable to
read the big endian fields (erroneous values because of the different
byte orders).

I am currently using FileStream and BinaryReader objects to open and
read the file. As I mentioned above calls to ReadInt32() return
erroneous values becuase of the byte order on the big endian fields.

Is there some sort of "magic" converter, like...

BigEndianToLittleEndian(r.ReadInt32());

that I can use to correctly get at the data?

What is the best method to use to crack this problem?

I've made some tests some time ago (try to search for "bigendian" and you'll
find the post/discussion).

The best method is: since you've reading a file, read the contents to a
byte[] array and then do this:

byte[] myArr; // where you read
int mSLittleEndian;

// i is the current index

mSLittleEndian = myArr | (myArr[i+1] << 8) | (myArr[i+2] << 16) |
(myArr[i+3]
<< 24);

That is faster than reading it to a int and do the "& 0xFF00000 >> 24" twist.

best
doc
 
Back
Top