BYTEORDER(3)

๐Ÿ’– BYTEORDER โ€” ok so bestie, let's talk htonl, htons, ntohl, ntohs ๐Ÿ’–

This is the BYTEORDER(3) page (yes that's the manual name, she's literally the title, iconic) โ€” and it's about the four little functions that make your bytes stop fighting each other and just get along. Converting values between host and network byte order. No cap. ๐Ÿฆ‹


๐Ÿ“š LIBRARY

Standard C library โ€” libc, -lc. She lives in libc, she's been there forever, she's practically a legacy queen ๐Ÿ‘‘


โœจ SYNOPSIS

#include <arpa/inet.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);

Four functions, four signatures, all of them completely chill. Copy them exactly, bestie, don't freestyle ๐Ÿ’…


๐ŸŒธ DESCRIPTION

Okay so here's the whole vibe. Your computer (the host) and the internet (the network) do NOT agree on which end of a number comes first. It's giving two friends who can't agree on where to sit at lunch. These four functions are the group chat mediator ๐Ÿ’–

  • htonl() โ€” takes that unsigned integer hostlong and converts it from host byte order to network byte order. h-t-o-n-l, "host to network, long." Long = 32-bit. Easy ๐Ÿ›
  • htons() โ€” same deal but for the unsigned short integer hostshort, going from host byte order to network byte order. s = short. Little guy. 16-bit. Tiny but fierce โœจ
  • ntohl() โ€” reverse direction, babe. Converts the unsigned integer netlong from network byte order to host byte order. n-t-o-h-l. Network to host. Long again. ๐ŸŽ€
  • ntohs() โ€” and the last one, converts the unsigned short integer netshort from network byte order to host byte order. Short one. Rounding out the squad. ๐Ÿซถ

Now the tea โ˜• โ€” on the i386, the host byte order is Least Significant Byte first, whereas the network byte order, as used on the Internet, is Most Significant Byte first. So yeah, they're opposites. That's the whole reason these functions exist, and honestly? She ate. ๐Ÿ”ฅ


๐Ÿงต ATTRIBUTES

For an explanation of the terms used in this section, see attributes(7). Go read it, I'm not doing your homework for you ๐Ÿ’€

  • htonl(), htons(), ntohl(), ntohs() โ€” Thread safety: MT-Safe ๐Ÿ‘‘

Translation: all four of them are MT-Safe, so you can call them from multiple threads and they will not start drama. Main character energy, every single one. No locks, no chaos, just vibes ๐Ÿ˜Œ


๐Ÿ“ STANDARDS

POSIX.1-2008. Certified. Standardized. On the list. Periodt ๐Ÿ’…


๐Ÿ•ฐ๏ธ HISTORY

POSIX.1-2001. She's been around a minute, lowkey a legend at this point ๐ŸŒˆ


๐Ÿ‘€ SEE ALSO

Go read those too. Expanding your knowledge is self care ๐Ÿงš๐Ÿ’œ


Okay that's the whole page, bestie!! You now understand byte order better than 90% of people who write network code, which is honestly a low bar but still, slay ๐Ÿ’–

Mwah ๐Ÿ’‹ โ€” this was Linux man-pages 6.20, dated 2026-02-08. Bye bestie, go convert some bytes ๐Ÿฆ‹โœจ

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms