Read and write from a byte stream if the endianess of the data is different from that of the current

Posted by Sam Holder on Stack Overflow See other posts from Stack Overflow or by Sam Holder
Published on 2010-03-29T14:55:24Z Indexed on 2010/03/29 15:43 UTC
Read the original article Hit count: 433

Filed under:
|

I have a stream of bytes which contains a flag which identifies the endianness of the data in the header. I want to read the doubles from the stream, which will presumably need to be different if the endianness of the data in the header is different?

I am currently using a BinaryReader and calling ReadDouble to read the data from the stream, but if the endianness flag indicates that the data stream has a different endianness than the machine architecture then presumably this will not work?

How should this be handled? Should I check the endianness of my data against that of the current machine then when I want to read a double instead read the bytes raw into a byte array and do array.Reverse to reverse the data before using BitConverter.ToDouble () with the reversed data and a zero offset?

I could just test this but I do not have a source of data for both endianness so am a bit concerned about creating test data to test the parsing and this being different from what 'real' data might look like.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about endianness