Big Endian / Little Endian

 

When transferring binary data between computers, it is important that both systems use the same data representations, or the information will be meaningless.  While it might appear to many that nothing could be simpler than binary integer numbers, there are at least two different formats for storing twos complement integers.  The difference depends upon the order in which the bytes of an integer are stored in memory.  The Big Endian format stores the most significant bytes (the bytes containing the high order or left most bits) in the lowest address with the following bytes in sequentially higher addresses.  The bytes would appear in normal order when written from left to right.  The Little Endian format stores the least significant byte (the byte containing the lowest or right most bits) at the lowest address with the increasingly significant bytes stored at increasing addresses.

 

For example, consider how the decimal number 258 (0100000010 in binary) would be stored in as a 16 bit binary number.

 

00000001

00000010

Big Endian

 

 

 

00000010

00000001

Little Endian

 

When transferring binary integers over a network, it is important to ensure that the sending and receiving systems use the same format or that a conversion is done.  If a Big Endian computer sent the above binary number to a Little Endian computer without conversion, it would be interpreted as 513.

 

To avoid problems in a heterogeneous network, binary numbers are converted to Network Standard Format (Big Endian) before transmission.  The functions:

 

htons            Host to Network Short

htonl            Host to Network Long

 

can be used to convert binary data to network standard format.

 

 

Historical note:  The names Big Endian and Little Endian come from Swift’s novel Gulliver’s Travels.  In this story the Lilliputians were divided into the Big Endians and the Little Endians based on which end of a boiled egg they believed should be opened.