Sockets in C: Little and Big Endian Machines

Part II in a beginner’s practical guide to creating a simple client-server application in C to be run locally (using TCP protocol).

Kyra Krishna
mycsdegree

--

Courtesy of Uy Nguyen

One important but annoying part of network programming is endianness. Little and Big Endian are two different ways of storing multi-byte data on machines (eg. int, long, etc.). In little endian machines, the least significant byte (LSB)is stored at the lowest address and the most significant byte (MSB) is stored at the highest address. This would look something like this:

Little Endian storing 1:
+-----------+-----------+-----------+-----------+
| 0000 0001 | 0000 0000 | 0000 0000 | 0000 0000 |
+-----------+-----------+-----------+-----------+
1001 1002 1003 1004
low high

Here, the least significant byte of 1 is 0000 0001 and is stored at the lowest address (x1001).

Conversely, Big Endian will store the most significant byte first at the lowest address and the least significant byte at the highest address:

Big Endian storing 1:
+-----------+-----------+-----------+-----------+
| 0000 0000 | 0000 0000 | 0000 0000 | 0000 0001 |
+-----------+-----------+-----------+-----------+
1001…

--

--

Kyra Krishna
mycsdegree

A computer science student with too many interests and just enough time to try them all.