PC Review


Reply
Thread Tools Rate Thread

Byte Order Woes

 
 
joey.powell@topscene.com
Guest
Posts: n/a
 
      23rd Jun 2006
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.

 
Reply With Quote
 
 
 
 
Vadym Stetsyak
Guest
Posts: n/a
 
      23rd Jun 2006
Hello, (E-Mail 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
 
Reply With Quote
 
Tom Spink
Guest
Posts: n/a
 
      23rd Jun 2006
(E-Mail Removed) wrote:

> 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
 
Reply With Quote
 
joey.powell@topscene.com
Guest
Posts: n/a
 
      23rd Jun 2006
Wow! Thanks man.

Vadym Stetsyak wrote:
> Hello, (E-Mail 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


 
Reply With Quote
 
=?Utf-8?B?ZG9jc2NobmlwcA==?=
Guest
Posts: n/a
 
      22nd Jul 2006
"(E-Mail Removed)" wrote:

> 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[i] | (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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
EXIF:UserComment Unicode byte-order bug osoviejo Windows Vista General Discussion 1 12th Mar 2008 11:13 AM
buffer data byte order Dave Microsoft VC .NET 0 18th Nov 2006 06:37 PM
byte order reversal in spreadsheet =?Utf-8?B?Q3VyaW91c01hcms=?= Microsoft Excel Programming 3 22nd Mar 2005 07:59 PM
byte order and endianness =?Utf-8?B?U2VudGhpbA==?= Microsoft C# .NET 1 5th Mar 2005 06:57 PM
No defined byte order for C#? Michi Henning Microsoft C# .NET 12 23rd Jan 2004 08:27 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:46 PM.