Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

Converting the byte order

&pagelevel(3)&pagelevel

If you use the address conversion functions described above, you will seldom have to directly handle addresses in an Internet user program. You can then develop services that are independent of networks to a large degree. However, some network dependency still remains as the IPv4 or IPv6 address has to be specified in a user program if a name is assigned to a service or socket.

In addition to the library functions for converting names to addresses, there are also macros which simplify handling names and addresses.

The host byte order and network byte order differ in some architectures. Because of this, programs sometimes have to change the byte order. The macros summarized in the following table convert long and short integer values from host byte order to network byte order and vice versa.

Call

Meaning

htonl(val)
htons(val)
ntohl(val)
ntohs(val)

Convert 32-bit fields from host byte order to network byte order
Convert 16-bit fields from host to network byte order
Convert 32-bit fields from network to host byte order
Convert 16-bit fields from network to host byte order

Table 1: Library macros for converting byte orders

The byte order conversion macros are needed because the library functions expect IPv4 addresses and port numbers in network byte order. The library functions that return network addresses supply them in network byte order, so they can be easily copied into data structures passed to other library functions when called.

However, problems can arise when interpreting network addresses.

With IPv6 addresses, there is by definition no difference between host byte order and network byte order, and there is therefore no corresponding conversion function.

The host and network byte orders are identical in BS2000. The macros listed in the table are therefore defined as null macros (macros without contents). However, it is strongly recommended that you use the macros for creating portable programs.