Which .Net-compatible architectures are not little-endian?
The BitConverter class has a field IsLittleEndian which, according to the documentation:
Indicates the byte order ("endianness") in which data is stor开发者_如何学Pythoned in this computer architecture.
I notice in Reflector that the field is hard-coded to true in the static constructor of BitConverter.
So my question is, do I need to take account of IsLittleEndian when using the BitConverter - in other words, are there any .Net implementations running on big-endian platforms? And if not, what was the purpose of the field in the first place?
The CLI standard does not proscribe any particular Endianness, so if you want your program to be portable, you should not depend on a particular byte order... unless of course in scenarios where a particular byte ordering is required, such as with some data exchange protocols (thanks to user The Moof for pointing this out).
From the CLI Annotated Standard (p.161) — Partition I, section 12.6.3: "Byte Ordering":
For data types larger than 1 byte, the byte ordering is dependent on the target CPU. Code that depends on byte ordering may not run on all platforms. [...]
I suspect that you saw a hard-coded value for IsLittleEndian
in Reflector because when you downloaded/installed the .NET Framework on your machine, that particular installation package was targeted at a particular platform (e.g. Intel x86, which is Little Endian).
I could thus imagine that there are other installation packages of the .NET framework that have IsLittleEndian
hard-wired to return a different value, depending on the platform that particular installation targets.
The .Net Micro Framework 4.1 supports big-endian - Source
SOME of the systems that the .NET Micro Framework runs on are (or can be...) big-endian. Version 4.1 introduced support for big-endian architectures.
You'd probably know if you were running on the micro framework, however...
Depends on what you mean by ".Net implementations". I'm not aware of Microsoft .NET Framework version for big-endian platform (but see other answers). But if you mean CLI/CLR implementation, then both Mono and DotGNU have versions for big-endian machines. Mono runs at least on Solaris 10 on Sparc and on Mac OS X on PowerPC. DotGNU has much wider list of available platforms.
If you're MSIL you don't get that guarantee.
Even if it's true for all current architectures, you have no guarantee I don't port .NET to Alpha or PDP11 tomorrow.
The XBox360 is BigEndian. Robert Unoki from Microsoft has a good blog post about the importance of checking the IsLittleEndian flag - and the bugs that occurred in porting the CLR to XBox360 when the Microsoft developers failed to do so.
精彩评论