Details about Endian-ness and .Net?

2020-05-30 03:03发布

问题:

I have a few questions about endian-ness that are related enough that I warrant putting them in as one question:

1) Is endian-ness decided by .Net or by the hardware?

2) If it's decided by the hardware, how can I figure out what endian the hardware is in C#?

3) Does endian-ness affect binary interactions such as ORs, ANDs, XORs, or shifts? I.E. Will shifting once to the right always shift off the least significant bit?

4) I doubt it, but is there a difference in endian-ness from different versions of the .Net framework? I assume they're all the same, but I've learned to stop assuming about some of the lower level details such as this.

If need be, I can ask these as different questions, but I figure anybody who knows the answer to one of these probably knows the answer to all of them (or can point me in a good direction).

回答1:

1) The hardware.

2) BitConverter.IsLittleEndian

3) Endianness does not affect bitwise operations. Shifting to the right is shifting in the least-significant-bit direction. UPDATE from Oops' comment: However, endianness does affect binary input and output. When reading or writing values larger than a byte (e.g., reading an int from a BinaryReader or using BitConverter), you do have to account for endianness. Once the values are read in correctly, then all bitwise operations act as normal.

4) Most versions of .NET are little endian. Notable exceptions include the XBox and some platforms supported by Mono or the Compact Framework.



回答2:

1) neither nor... or you can say either
it was decide by the hardware developers. And you must decide about it if you write software that read/writes certain file formats without using external libraries.
There is no problem with endiannes If you read from a file
* a byte
but you have to decide how to interprete it in little or big endian format if you read all other primitive data types like
* integers,
* strings,
* floats.
The hardware does not help here. The BitConverter does not help here. Only the documentation of the file format could help and testing your code of course...
edit: found ad good explanation here: http://betterexplained.com/articles/understanding-big-and-little-endian-byte-order/



标签: c# endianness