little or big endian in c#

If you want to check the endianess of your operating system in c# you have two possibilities, a safe and a unsafe way. But first of all i want to explain what endianess is and what big and little endian means. If you have an Int32 value in c#, that Int32 is saved in 4 bytes. But the question is which part of the Int32 is saved in which byte with which address. If the system is using little endian (like Windows does) the last byte (=least significant byte) is saved in the lowest address. That means if you have the hex value 0A0B0C0D and you have the Int32 has the address 00000001, the bytes are saved in the following order:

0D at address 00000001

0C at address 00000002

0B at address 00000003

0A at address 00000004

The following images shows the insertion order:

LittleEndian

If the system has big endian order the last byte (=least significant byte) is saved in the highest address. That means the Int32 value 0A0B0C0D with the address 00000001 the bytes are inserted the following way:

0A at address 00000001

0B at address 00000002

0C at address 00000003

0D at address 00000004

The following image shows the insertion order:

BigEndian

Now if we want to check if the system uses big or little endian in c# there is a very easy way, you can use the BitConverter class that has an IsLittleEndian property.

if(BitConverter.IsLittleEndian)
   return "Little Endian";

If you want to implement the check yourself the simplest method is the following one:

public enum BigLittleEndianEnum
{
    Big,
    Little
}
public BigLittleEndianEnum TestSystemIsBigOrLittleEndianSafe()
{
    Int32 intValue = 0x0001;
    byte[] bytes = BitConverter.GetBytes(intValue);
    var safeBigLittleEndian = (bytes[0] == 1) ? 
        BigLittleEndianEnum.Little : BigLittleEndianEnum.Big;
    return safeBigLittleEndian;
}

What we do is we take the Int32 type intValue and assign 1 to it. Then we use the BitConverter class to generate a byte array out of that Int32 value. If the zero byte of the array (byte[0]) is one the we know that the system uses little endian because the 1 is in the lowest address (byte[0]).
If you search for check little or big endian in google you often find the c/c++ solution. In c# you have to use the unsafe keyword, because you need pointer syntax to implement the c/c++ solution.

public BigLittleEndianEnum TestSystemIsBigOrLittleEndianUnsafe()
{
    Int32 intValue = 0x0001;
    unsafe
    {        
        Int32* intValueAddress = &intValue;                
        byte* byteValueAddress = (byte*) intValueAddress;
        byte byteValue = *byteValueAddress;
        var unsafeBigLittleEndian = (byteValue == 1) ? 
            BigLittleEndianEnum.Little : BigLittleEndianEnum.Big;        
        return unsafeBigLittleEndian;                        
    }            
}

Here we use a pointer of the Int32 intValue and cast it to a byte pointer. That byte pointer points on the lowest address (byte[0]) of the Int32 value and you can check if the value the pointer points on is zero or one, if it is one its little endian. A real c/c++ programmer would write that whole method in two lines:

public BigLittleEndianEnum TestSystemIsBigOrLittleEndianUnsafe()
{
    Int32 intValue = 0x0001;
    return *((byte*) &intValue) == 1 ? 
        BigLittleEndianEnum.Little : BigLittleEndianEnum.Big;
}

For a more detailed explanation please look at the wickipedia article about Endianess.
 

Advertisements


If you have a note or a question please write a comment.

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s