- Byte Order and Network order
- Significance of Byte ordering
- Little-endian and Big-endian
- Network Program
Byte Order:
- Byte Ordering is an attribute of the processor which defines how data are arranged or ordered.
- Byte Ordering is important when data is transferred across a network or between system using different Ordering.
Network Order:
Network Byte Order refers to how bytes are arranged when sending data over a network.
Host Byte Order:
This refers to how bytes are arranged on a computer system.
Significance of Byte Ordering:
- A byte is of 8 bits and has a limited range of 256 values.
- When the value is beyond this range, it has to be stored in multiple bytes.
- A number such as 258 in hexadecimal format is 0x0102 and required minimum 2 bytes.
- The order in which these two bytes are stored in memory can be different depending upon the processor.
- Some processor stores the values in lower memory address followed by higher memory address.
Example:
Decimal Values : 258
Hex : 0x0102
- Lower memory address followed by Higher memory : 0x01 followed by 0x02
- Higher memory address followed by Lower memory : 0x02 followed by 0x01
Thus if Byte Ordering is not appropriate according to the processor, the 0x0102 can be wrongly interpreted as 0x0201 which is 513 in decimal system.
Little-Endian and Big-Endian:
There is two common Ordering System:
- Little Endian and
- Big Endian
Little Endian:
- In this least-significant(Low-order) byte is stored at a lower address.
- Example: Intel Processor
Big Endian:
- In this the most significant(High-order) byte is stored at lower address.
- Example: IBM PowerPC or Motorola PowerPC Processor
Some Processor support both ordering and therefore Bi-Endian and are configurable. MostRISC architectures(SPARC, MIPS) are configurable now.
Diag-1: Little-endian and Big-endian

Comparing Little-Endian and Big-Endian Ordering
Host and Network Byte Order API:
- For the correct interpretation of values across the system or a network, the network program must convert their internal representation(Host Byte Order) to Network Byte order(Big-Endian).
- A host system may be little-endian but when sending data to a network, it must convert data into big-endian.
- Likewise, a little-endian machine must first convert network data into little-endian before processing.
- This can be done with a few systems call API defined in netinet/in.h are(for 32 bit long and 16-bit short)
- unsigned long int htonl ( unsigned long int hostlong);
- unsigned long int ntohl (unsigned long int netlong);
- unsigned short int htons (unsigned short int hostshort);
- unsigned short int ntohs (unsigned short int netshort);
- Two important parameters in socket programming is Port number(16 bits) and IP address(32 bits), these two must be ordered correctly.
- For port which is 16 bits, htons() and ntohs() can be used and for IP address of 32 bits htonl() and ntohl() can be used.
Diag-2: htonl-to-ntohl

Illustrating the use of htonl and ntohl function
In the AF_INET domain, the address is specified using a structure called sockaddr_in, defined in netinet/in.h, which contains at least three members.
struct sockaddr_in { short int family; /*AF_INET */ unsigned short int sin_port; /* Port Number */ struct in_addr sin_addr; /* Internet Address */ }; struct in_addr { unsigned long int s_addr; };
Program to convert the Port and IP Address into Network Order(Big-Endian)
#include <stdio.h> #include <stdlib.h> #include <netinet/in.h> int main() { struct sockaddr_in addr; unsigned short int port = 1234; unsigned long int ip = 2110443574; addr.sin_port= (htons (port)); addr.sin_addr.s_addr = htonl(ip); /* Host Order(Little-Endian) */ printf("\n Port in Host Order(Little-Endian) Hex: %x Decimal: %d\n",port, port); printf("\n IP in Host Order(Little-Endian) Hex: %lx Decimal: %lu\n",ip, ip); /* Network Order(Big-Endian) */ printf("\n Port in Network Order(Big-Endian) Hex: %x Decimal: %d \n",addr.sin_port,addr.sin_port); printf("\n IP address in Network Order(Big-Endian) Hex: %x Decimal: %d \n",addr.sin_addr.s_addr,addr.sin_addr.s_addr); return 0; }
Output:
Port in Host Order(Little-Endian) Hex: 4d2 Decimal: 1234 IP in Host Order(Little-Endian) Hex: 7dcad036 Decimal: 2110443574 Port in Network Order(Big-Endian) Hex: d204 Decimal: 53764 IP address in Network Order(Big-Endian) Hex: 36d0ca7d Decimal:919652989
Program to check the Endiness of the system:
#include <stdio.h> int main () { unsigned int x = 0x0102; char *c = (char*) &x; printf ("\n Value at c is: 0x%x\n", *c); if (*c == 0x02) printf ("Underlying architecture is little endian. \n") else printf ("Underlying architecture is big endian. \n"); return 0; }
Relevant Posts:
- Sockets Introduction
- Socket APIs
- TCP Socket Program
- UDP Socket Program
- Socket Options
- Multi-client server support- select() API
- Host and Network Byte ordering
Categories: Operating system (OS)
Leave a Reply