htons(3)

htons3

Convert values between host and network byte order

Part of byteorder(3).

Usage

#include <arpa/inet.h>

or

#include <netinet/in.h>

uint32_t

htonl(uint32_t hostlong);

uint16_t

htons(uint16_t hostshort);

uint32_t

ntohl(uint32_t netlong);

uint16_t

ntohs(uint16_t netshort);

Library

Standard C Library (libc, -lc)

Overview

These routines convert 16 and 32 bit quantities between network byte order and host byte order. On machines which have a byte order which is the same as the network order, routines are defined as null macros.

These routines are most often used in conjunction with Internet addresses and ports as returned by gethostbyname(3) and getservent(3).

Releases

87 releases since FreeBSD 2.0.5-RELEASE; the text changed 42 times.

  1. 2.0.5–3.4
  2. 3.5–3.5.1
  3. 4.0
  4. 4.1–4.3
  5. 4.4
  6. 4.5–4.11
  7. 5.0–5.2.1
  8. 5.3
  9. 5.4–5.5
  10. 6.0–6.3
  11. 6.4
  12. 7.0
  13. 7.1
  14. 7.2
  15. 7.3
  16. 7.4
  17. 8.0
  18. 8.1
  19. 8.2
  20. 8.3
  21. 8.4
  22. 9.0
  23. 9.1
  24. 9.2
  25. 9.3
  26. 10.0
  27. 10.1
  28. 10.2
  29. 10.3
  30. 10.4
  31. 11.0
  32. 11.1
  33. 11.2
  34. 11.3
  35. 11.4
  36. 12.0
  37. 12.1
  38. 12.2
  39. 12.3
  40. 12.4
  41. 13.0–13.2
  42. 13.3–14.5
  43. 15.0–16.0

Standards

The byteorder functions conform to IEEE Std 1003.1-2001 (“POSIX.1”).

History

The byteorder functions appeared in 4.2BSD.

Bugs

On the VAX bytes are handled backwards from most everyone else in the world. This is not expected to be fixed in the near future.

From base 16.0-CURRENT-20261027-61c70dc16a79 in FreeBSD 16.0-CURRENT (2026-10-27). Parsed by carta from the page's mdoc source.