endian(3)
β¨ endian(3) β¨
okay bestie so this is the endian(3) page from the Library Functions Manual, and it's literally all about the cute little glow-up where you flip integer bytes between whatever your CPU thinks is normal and big-/little-endian. no cap, it's giving very "translator for the group chat." π
π NAME π
htobe16, htole16, be16toh, le16toh, htobe32, htole32, be32toh, le32toh, htobe64, htole64, be64toh, le64toh β these cuties convert values between host and big-/little-endian byte order π¦
π LIBRARY π
Standard C library (libc, -lc) π
πΈ SYNOPSIS πΈ
#include <endian.h>
uint16_t htobe16(uint16_t host_16bits);
uint16_t htole16(uint16_t host_16bits);
uint16_t be16toh(uint16_t big_endian_16bits);
uint16_t le16toh(uint16_t little_endian_16bits);
uint32_t htobe32(uint32_t host_32bits);
uint32_t htole32(uint32_t host_32bits);
uint32_t be32toh(uint32_t big_endian_32bits);
uint32_t le32toh(uint32_t little_endian_32bits);
uint64_t htobe64(uint64_t host_64bits);
uint64_t htole64(uint64_t host_64bits);
uint64_t be64toh(uint64_t big_endian_64bits);
uint64_t le64toh(uint64_t little_endian_64bits);
π§ Feature Test Macro Requirements for glibc (see feature_test_macros(7)): π§
htobe16(), htole16(), be16toh(), le16toh(), htobe32(), htole32(), be32toh(), le32toh(), htobe64(), htole64(), be64toh(), le64toh():
- Since glibc 2.19:
_DEFAULT_SOURCE
- In glibc up to and including 2.19:
_BSD_SOURCE
lowkey important: those macros are how you unlock these functions, so if the compiler is acting brand new and refusing to see them, this is the vibe check you failed. π
π DESCRIPTION π
Bestie, here's the tea β β these functions convert the byte encoding of integer values from the byte order that the current CPU (aka the "host") uses, to and from little-endian and big-endian byte order. that's it, that's the whole job. iconic behavior honestly. π
The number, nn, in the name of each function indicates the size of integer handled by the function, either 16, 32, or 64 bits.
The functions with names of the form "htobenn" convert from host byte order to big-endian order. π₯
The functions with names of the form "htolenn" convert from host byte order to little-endian order. π©·
The functions with names of the form "benntoh" convert from big-endian order to host byte order. π
The functions with names of the form "lenntoh" convert from little-endian order to host byte order. π
not me explaining it like a decoder ring but fr fr that's exactly what it is: read the name left to right, that's the direction you're going. π
π¦ VERSIONS π¦
Similar functions are present on the BSDs, where the required header file is <sys/endian.h> instead of <endian.h>. Unfortunately, NetBSD, FreeBSD, and glibc haven't followed the original OpenBSD naming convention for these functions, whereby the nn component always appears at the end of the function name (thus, for example, in NetBSD, FreeBSD, and glibc, the equivalent of OpenBSDs "betoh32" is "be32toh"). π
the way that everybody just did their own thing with the naming... nobody agreed on the group chat and now we all live like this. π«
β STANDARDS β
None.
yeah. none. it's giving "not standardized, bestie, we just vibe." π
π HISTORY π
glibc 2.9. β¨
These functions are similar to the older byteorder(3) family of functions. For example, be32toh() is identical to ntohl().
The advantage of the byteorder(3) functions is that they are standard functions available on all UNIX systems. On the other hand, the fact that they were designed for use in the context of TCP/IP means that they lack the 64-bit and little-endian variants described in this page. π
πΈ EXAMPLES πΈ
The program below displays the results of converting an integer from host byte order to both little-endian and big-endian byte order. Since host byte order is either little-endian or big-endian, only one of these conversions will have an effect. When we run this program on a little-endian system such as x86-32, we see the following:
$ ./a.out;
x.u32 = 0x44332211
htole32(x.u32) = 0x44332211
htobe32(x.u32) = 0x11223344
see how htole32 handed the value back untouched? that's because the host was already little-endian. the other conversion did all the work. main character energy from exactly one of them. π
π§ Program source π§
#include <endian.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
int
main(void)
{
union {
uint32_t u32;
uint8_t arr[4];
} x;
x.arr[0] = 0x11; /* Lowest-address byte */
x.arr[1] = 0x22;
x.arr[2] = 0x33;
x.arr[3] = 0x44; /* Highest-address byte */
printf("x.u32 = %#x\n", x.u32);
printf("htole32(x.u32) = %#x\n", htole32(x.u32));
printf("htobe32(x.u32) = %#x\n", htobe32(x.u32));
exit(EXIT_SUCCESS);
}
π«Ά SEE ALSO π«Ά
and that's the whole page bestie, you ate it π go forth and byte-swap responsibly, periodt. π¦β¨
this little love letter comes to you from Linux man-pages 6.20, dated 2025-05-17 β mwah, take care of her ππ
π¦ 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