36.1 Network and Host Byte Order Functions

Byte order is dependent on the CPU Word architecture. In processors compatible with Intel processors, 4 byte long integers are represented as a sequence of 4 bytes, with the less significant bytes of the integer stored in the more significant bytes. This byte order is called host byte order or little endian order (little-end-first). For example, a host byte order machine would store a 0x01020304 value in memory in the following order:

     04 03 02 01
  

In contrast, most mainframe CPUs, Motorola CPUs, and RISC-designed CPUs store 4 byte long integers with the most significant bytes of an integer before the less significant bytes. This byte order is called network byte order or big endian order (big-end-first). For example, a network byte order machine would store a 0x01020304 value in memory in the following order:

     01 02 03 04
  

By default, heterogeneous TCP/IP networks transmit integers, IP addresses, and port number in network byte order. Because NetWare® runs on host byte order machines, you will need to switch between host byte order and network byte order.

The following functions allow you to switch integers between these two formats.

Function

Description

htonl

Converts 32-bit quantities from host to network byte order.

htons

Converts 16-bit quantities from host to network byte order.

ntohl

Converts 32-bit quantities from network to host byte order.

ntohs

Converts 16-bit quantities from network to host byte order.