Your Browser is not longer supported

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

{{viewport.spaceProperty.prod}}

Byte order macros - convert byte order

&pagelevel(3)&pagelevel

#include <sys.types.h>
#include <netinet.in.h>

u_long htonl(u_long hostlong);

u_short htons(u_short hostshort);

u_long ntohl(u_long netlong);

u_short ntohs(u_short netshort);


Description

The htonl(), htons(), ntohl() and ntohs() macros are only required in the AF_INET and AF_INET6 address families. htonl(), htons(), ntohl() and ntohs() convert bytes and integers of the type integer or short from host byte order to network byte order and vice versa:

  • htonl() converts 32 bit fields from host to network byte order.

  • htons() converts 16 bit fields from host to network byte order.

  • ntohl() converts 32 bit fields from network to host byte order.

  • ntohs() converts 16 bit fields from network to host byte order.

These macros are mainly used in connection with IPv4 addresses and port numbers, e.g. as returned by the gethostbyname() function (see section "gethostbyaddr(), gethostbyname() - get information about host names and addresses").

With regard to IPv6 addresses, a decision was made according to RFC 2553 in favor of the guaranteed network byte order. Therefore only the 16-bit byte order macros are required for the port numbers for the AF_INET6 address family.

The macros are only needed on systems on which the host and network byte orders differ. Since the host and network byte orders are identical in BS2000, the macros are supplied as null macros (macros without a function) in the <netinet.in.h> header file.

Note, however, that the use of byte order macros is strongly recommended if you want to create portable programs.

Return value

htonl() and htons() return the input parameter after conversion into network byte order.

ntohl() and ntohs() return the input parameter after conversion into host byte order.

See also

gethostbyaddr(), gethostbyname(), getservbyname()