开发者

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

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 f开发者_开发问答rom 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.


if the endianness flag indicates that the data stream has a different endianness than the machine architecture then presumably this will not work?

First - the bit order is abstracted from you. Unless you are twiddling bits, you don't care.

The byte order is a different thing. For a 4-byte quantity, it can be least-significant-byte first or most-significant-byte first. I don't know what the ReadDouble() uses, but if you are using a BinaryReader on one end of the wire and a BinaryWriter on the other, then once again, you don't care. It should be written in the same order as it is read, without regard for endianness.

The only case where you care, it seems to me, is if you don't use .NET on the other end of the wire. In that case you must make sure that the bytes are in the proper order, using a scheme similar to what you described. (Array.Reverse, etc).

Normally applications that face this problem use a multi-byte signature to indicate clearly the order. In other words, write 0x01020304 to the stream, and on the reader side, if you get 0x04030201, you know you've got a difference in endianness.

I could just test this but I do not have a source of data for both endianness ...

Well you have to fix that. You can't develop without a capability to test what you're developing. It also begs the question: if you don't have a source that would use different endianness, then why are you worried about the issue at all?

See also:
- Reversing byte order in .NET
- A class for choosing the endianness of BitConverter


BinaryReader reads data (including doubles) in little endian :

BinaryReader..::.ReadDouble

BinaryReader reads this data type in little-endian format.

If you want to read BigEndian data, you may override BinaryReader methods or add overloads with an enum argument to specify the expected endianness.

To implement bing endian reading, you may look at Cheeso answer or do some reverse engeneering.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜