Monday, April 27, 2009

Discovering System Endianess

When doing network programming we send bytes to and from peers. These bytes sometimes constitute complex protocols.

Let us assume we have simple message exchange protocol with some header and some data. Like in the picture here.

Our prefix contains the size of the message. When peer receives bytes from network it reads header first (which is fixed length) then decodes information from the header (data size etc) and finally tries to read the specified number of bytes from the network.

Network applications usually have at least two peers. These peers can be hosted on different systems. Say, peer1 is working on Windows OS, while peer2 - is Java app working in Unix environment.
Our protocol contains integer value that holds message's data size. This value is 4-bytes long.

Windows OS works with numbers that are considered to be little endian, that is least significant byte is placed on the lowest address. Vice versa for Java number. This division is known as endianess.

To transfer multi-byte values over network in uniform manner an agreement was established. Multi-byte data is written into network in big endian byte order.

Earlier I have said that Windows is little endian system, do you really believe me?
If you do not - check yourself, here's how you can do this in C#.

First approach:

int number = 0x00000001;
byte[] bytes = BitConverter.GetBytes(number);
bool isBigEndian = bytes[0] == 0x00;
Second approach (for geeks):
int number = 0x00000001;
int* p = &number;
bool isBigEndian = p[0] == 0x00;
And finally the third one:
bool isBigEndian = !BitConverter.IsLittleEndian;
When you want to write self-contained code, the above approaches can be used to determine endianess of the system your code operates on.

P.S. Third method is the best :).


  1. Wow! This is all messed up. :-(

    From Wikipedia: "processor architectures that use the little-endian format include x86"

    On my Windows box:
    >>> System.Console.WriteLine(System.BitConverter.IsLittleEndian)

  2. Yep, my miss here.
    Windows, indeed uses little endian format. While in networking big endian is used.

    Thanks for the comment :)

  3. Two more. :-,)

    s/bool bigEndian/bool littleEndian/