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(4)&pagelevel

#include <arpa/inet.h>

in_addr_t htonl(in_addr_t hostlong);
in_port_t htons(in_port_t hostshort);
in_addr_t ntohl(in_addr_t netlong);
in_port_t ntohs(in_port_t netshort);

Description

The htonl(), htons(), ntohl() and ntohs() macros convert shorts and integers from host byte order to network byte order and vice versa.

The data type definitions in in_addr_t and in_port_t in <arpa/inet.h> correspond to the definitions in <netinet/in.h>.

These macros are used in conjunction with IPv4 addresses and port numbers, e.g. as returned by the gethostbyname() and getservent() functions. The macros are only needed on systems on which the host and network byte orders are different and are provided in the <arpa/inet.h> header file as null macros (macros without functions) for POSIX/BS2000:

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

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

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

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

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(), gethostent(), getservent()