BYTEORDER(9)
π BYTEORDER β byte order operations π¦
okay bestie, welcome to BYTEORDER(9), straight out of the Kernel Developer's Manual. this is the page where the kernel tells you how to flip your bytes around, and honestly? it's giving essential. if you've ever stared at a hex dump from a big endian machine and felt your soul leave your body, this is for you. β¨
πΈ SYNOPSIS πΈ
#include <sys/endian.h>
uint16_t
bswap16(uint16_t int16);
uint32_t
bswap32(uint32_t int32);
uint64_t
bswap64(uint64_t int64);
uint16_t
be16toh(uint16_t big16);
uint32_t
be32toh(uint32_t big32);
uint64_t
be64toh(uint64_t big64);
uint16_t
htobe16(uint16_t host16);
uint32_t
htobe32(uint32_t host32);
uint64_t
htobe64(uint64_t host64);
uint16_t
htole16(uint16_t host16);
uint32_t
htole32(uint32_t host32);
uint64_t
htole64(uint64_t host64);
uint16_t
le16toh(uint16_t little16);
uint32_t
le32toh(uint32_t little32);
uint64_t
le64toh(uint64_t little64);
uint16_t
be16dec(const void *);
uint32_t
be32dec(const void *);
uint64_t
be64dec(const void *);
uint16_t
le16dec(const void *);
uint32_t
le32dec(const void *);
uint64_t
le64dec(const void *);
void
be16enc(void *, uint16_t);
void
be32enc(void *, uint32_t);
void
be64enc(void *, uint64_t);
void
le16enc(void *, uint16_t);
void
le32enc(void *, uint32_t);
void
le64enc(void *, uint64_t);
β¨ DESCRIPTION β¨
okay so the whole cast is here: bswap16(), bswap32(), and bswap64() hand you back a byte order swapped integer. on big endian systems, the number gets converted to little endian byte order. on little endian systems, the number gets converted to big endian byte order. it's a flip, it's a glow-up, it's whatever endianness you weren't. π«
then we have be16toh(), be32toh(), and be64toh(), which take a big endian byte ordered integer and convert it to the system's native byte order. plot twist: on big endian systems the return value will be the same as the argument. no work, no stress, just vibes. π«Ά
le16toh(), le32toh(), and le64toh() do the mirror thing β they take a little endian byte ordered integer and convert it to the system's native byte order. and yeah, on little endian systems the return value will be the same as the argument. icon behaviour, truly. π
going the other way now: htobe16(), htobe32(), and htobe64() return an integer in the system's native byte order converted to big endian byte order. on big endian systems, again, the return value will be the same as the argument. periodt. π
and htole16(), htole32(), and htole64() return a integer in the system's native byte order converted to little endian byte order. on little endian systems the return value will be the same as the argument. yes bestie, that's the pattern, you're getting it, i'm so proud. π
finally the encode/decode squad: be16enc(), be16dec(), be32enc(), be32dec(), be64enc(), be64dec(), le16enc(), le16dec(), le32enc(), le32dec(), le64enc(), and le64dec(). these ones encode and decode integers to/from byte strings on any alignment, in big or little endian format. any alignment!! the unaligned girlies are SEEN. π₯
ngl, the whole "same as the argument" thing is the reason these are basically free on the matching architecture. the compiler ate that. β¨
π SEE ALSO π
byteorder(3)β go read this one too, it's the sibling, the bestie, the other half of the duo π
π¦ HISTORY π¦
the hto*() and *toh() functions first appeared in FreeBSD 5.0, and were originally developed by the NetBSD project. so they've been around, they've got lore, they've got range. π
the encode/decode functions first appeared in FreeBSD 5.1. slightly younger sibling energy but equally iconic. π
and that's the page bestie!! this version of the manual comes to you from Debian, dated April 29, 2002. old money, honestly. now go flip some bytes and slay. πβ¨π
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 2002 Mike Barcroft <mike@FreeBSD.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms