4

The BitConverter class has a field IsLittleEndian which, according to the documentation:

Indicates the byte order ("endianness") in which data is stored 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?

Samuel Jack
  • 32,712
  • 16
  • 118
  • 155

6 Answers6

4

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...

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Mark
  • 11,257
  • 11
  • 61
  • 97
4

The .Net Micro Framework 4.1 supports big-endian - Source

Matthieu
  • 4,605
  • 4
  • 40
  • 60
3

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.

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
  • 1
    IP packets are extremely byte-order dependent, so you need to keep that in mind when talking about portability. It's rare to have a project come through that needs low-level network programming, but they're out there. – The Moof Sep 10 '10 at 15:18
3

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.

Tomek Szpakowicz
  • 14,063
  • 3
  • 33
  • 55
1

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.

Joshua
  • 40,822
  • 8
  • 72
  • 132
1

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.

Samuel Jack
  • 32,712
  • 16
  • 118
  • 155