function::htonl - Linux


The function::htonl function in Linux is used to convert a 32-bit integer (host byte order) to a network byte order (big-endian). This is useful when working with network protocols, where data needs to be in network byte order for proper transmission and interpretation.


::htonl(int hostlong) -> long




Example 1: Convert a host byte order integer to network byte order.

#include <netinet/in.h>

int main() {
  uint32_t host_long = 0x12345678;
  uint32_t network_long = htonl(host_long);
  printf("Host byte order: 0x%x\n", host_long);
  printf("Network byte order: 0x%x\n", network_long);
  return 0;


Host byte order: 0x12345678
Network byte order: 0x78563412

Common Issues



The function::htonl function can be used in conjunction with the function::ntohl function to convert between host and network byte orders. This is particularly useful when working with network protocols that use big-endian byte ordering, such as TCP/IP.

Related Commands

  • function::ntohl: Converts a network byte order integer to host byte order.
  • function::htons: Converts a 16-bit integer from host byte order to network byte order.
  • function::ntohs: Converts a 16-bit integer from network byte order to host byte order.