# Little endian to Big endian conversion and its Importance

Generally, people who work on the high-level language do not take the interest in details of computer architecture and ignore the concept of endianness and also never think about the concept to convert little endian to big endian. But sometimes endianness became crucial issues in your program when you transmit data serially over the network from one computer to another computer.

So endianness is mainly important when you are reading and writing the data across the network from one system to another. If the sender and receiver computer have the different endianness, then receiver system would not receive the actual data which transmitted by the sender.

So to handle this scenario here, I am describing some important concept related to the endianness.

#### Endianness

The endianness is the bytes order in which data stored in the memory of the computer and it also describes the order of byte transmission over a digital link. In memory data store in which order it depends on the endianness of the system, if the system is big-endian then the MSB byte store first (means at lower address) and if the system is the little endian then LSB byte store first (means at lower address).

Some examples of little-endian and big-endian system.

#### Storing of bytes in memory

In the previous article, “data alignment” we have already know that processor performs the operation on the word ( the word can be 1, 2, 4 bytes), here I suppose word size is 4 bytes and data which need to store in memory is 32 bits means 4 bytes.

How is the data stored in memory it depends on the endianness of processor?

Suppose, 32 bits Data is 0x11223344.

#### Big-endian

The most significant byte of data stored at the lowest memory address.

#### Little-endian

The least significant byte of data stored at the lowest memory address.

Note:  Some processor have the ability to switch one endianness to other endianness using the software means it can perform like both big endian or little endian at a time. This processor is known as the Bi-endian, here are some architecture (ARM version 3 and above, Alpha, SPARC) who provide the switchable endianness feature.

#### Program to check the endianness of processor

We have already familiar with memory layout of the big-endian and little-endian system, if the system is little endian then LSB byte store at lower address either in the case of big-endian MSB stored at the lower address. So here to check the endianness of running system, we write a simple c program and check its lower address value. If the value of lower address is LSB byte then the system is little endian either the system is big-endian.

To check the endianness of running system using the union, you have to create a union of an integer and array of 4 characters. If the first element (au8DataBuff [0]) of the character array is equal to the LSB of data, then the system will be little endian otherwise big-endian.

#### Process  to change the endianness

A lot of API and online tools are available to convert little endian to big endian and big endian to little endian.

Here I am describing some example to how to switch from one endianness to another endianness.

We can also make the macro to swap the data from one endianness to another.

Using the union we can also change the endianness of data.

#### How is the endianness affect the code?

When you perform a bit-wise operation on integer then compiler automatically handle the endianness and you don’t need to care about the endianness of the machine.
After the bit-wise operation if the machine is little endian the LSB store at lower address either the LSB stored at a higher address.

Mainly endianness affects the result when you perform the typecasting in your program, suppose you are creating a character array of four elements and you need to convert character array in a single integer element then what will be the result? It depends on the endianness of the processor.

If you compile this code on a little-endian processor then the output will be 0x01 but if you compiled it on the big-endian processor then the output will be 0x01000000.
Endianness also plays a vital role, when you will send the data from the one system to another system across the network. In this situation, you have to swap the data if the endianness of receiving system different from the transmitting system.

#### Exchanging Data Between Endian Machines

Nowadays every system which is attached to one another is either little endian or big endian. Everything is fine till you haven’t sent any data to the network because if the endianness of transmitting and the receiving system is different then it might cause data loss. So to avoid the culprit, I am describing the
ways to how to prevent the data loss.

##### Sent the data in common format

This approach is very simple, whenever we send the data through the network then we don’t know receiving machine is big endian or little endian, so we have to send the data in common format. The standard network order is big-endian, we can call it “network order”.

There are a lot of function which has used when we send the data to the network.
htons() – “Host to Network Short”
htonl() – “Host to Network Long”
ntohs() – “Network to Host Short”
ntohl() – “Network to Host Long”

The above-mentioned functions help to program in a portability no matter if the program runs on a little-endian or big-endian machine, they always work the way they should.
Note:  There is no order matter for the single byte.

Let’s see an example for the better understanding.

Suppose there are two machine S1 and S2, S1 and S2 are big-endian and little-endian relatively. If S1(BE) wants to send 0x44332211 to S2(LE), the following scenario occurred.

• S1 has the number 0x44332211, it will store in memory as following sequence 44 33 22 11.
• S1 calls htonl() as the program has been written to be portable. The number is still represented as 44 33 22 11 and sent over the network.
• S2 receives 44 33 22 11 and calls the ntohl().
• S2 gets the value represented by 11 22 33 44 from ntohl(),  which then results to 0x44332211 as wanted.

Note: I have already explained in little-endian lower bytes stored at the lower address.

#### Which Endianness better?

There is no meaning to say who is the better big endian or little endian, it only arranges the data in a predefined order.

#### Little Endian

In the case of little endian, you can access first bytes of data with zero offsets because LSB stored at the lower address. Hence in case of the little endian relation between address and offset is 1:1 and easy to access the data.

#### Big Endian

In the case of big-endian MSB byte stored at the lower address, so we can access the MSB byte with zero offsets and check the sign of data without the knowing the length of data.

Note: Endianness is not associated with register means register is neither big endian and little endian it only store the value of data in a bit and its left most bit is MSB bit and rightmost bit is LSB bit.

#### Conclusion

We find there is no advantage of using one endianness over the other, both only define the byte sequence order. Today mostly personal computer and desktop come in little-endian architecture. Endian also do not affect the single byte it only affects the multi-byte data because we consider byte is the atomic unit from the storage point of view.

Although here I have tried to discuss a lot of points regarding the little-endian and big-endian but I would like to know your opinion on the system endianness. So please don’t forget to write a comment in the comment box.

1. Rohit Kamat says:

good topics expained well !! awaiting for new blogs

2. Sanjay Jha says:

Excellent step by step example to differentiate both little endian and big endian. keep it up. 🙂

3. Rashmi says:

Very simplified and understandable! Looking forward for new embedded concepts!

1. Amlendra says:

Thank you so much Rashmi..