byteorder(3bsd)

πŸ“¦
version 0.12.2 Β· 2 versions
  • 0.12.2 Β· alpine 3.22, alpine 3.23, alpine 3.24, arch rolling, debian bookworm, debian trixie, epel 10, epel 9, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0, ubuntu noble, ubuntu questing, ubuntu resolute, ubuntu stonking (latest)
  • 0.11.5 Β· ubuntu jammy
, in alpine 3.22, alpine 3.23, alpine 3.24, arch rolling, debian bookworm, debian trixie, epel 10, epel 9, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0, ubuntu noble, ubuntu questing, ubuntu resolute, ubuntu stonking

✨ byteorder β€” the manual page bestie πŸ’…

okay so this is byteorder(3bsd), from the LOCAL section, and it's literally the whole "move numbers in and out of bytes without caring where they sit in memory" situation. no cap, it's short, it's iconic, let's go πŸŽ€

πŸ’– NAME πŸ’–

be16enc, be16dec, be32enc, be32dec, be64enc, be64dec, le16enc, le16dec, le32enc, le32dec, le64enc, le64dec β€” byte order operations πŸ¦‹

πŸ“š LIBRARY πŸ“š

Utility functions from BSD systems β€” that's libbsd, and you link it with -lbsd. yes, the whole point is you get the BSD goodies. periodt πŸ’œ

🌸 SYNOPSIS 🌸

#include <sys/endian.h>

(btw, peep libbsd(7) for how the include situation actually works β€” it's giving "read the fine print" energy πŸ™ˆ)

okay here are the decoders, they hand you a number back:

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 *);

and here are the encoders, they return void because they're busy writing into your buffer, no time to chat:

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 🧚

so listen. be16enc(), be16dec(), be32enc(), be32dec(), be64enc(), be64dec(), le16enc(), le16dec(), le32enc(), le32dec(), le64enc(), and le64dec() encode and decode integers to/from byte strings on any alignment in big/little endian format.

that's the whole vibe, bestie. you got your be squad (big endian) and your le squad (little endian), 16/32/64 flavors each, and the any alignment part means you do NOT have to sit there stressing about whether your pointer is aligned. it just works. main character energy fr fr πŸ’…


🫢 reminder: if you're reaching for these, double-check which endianness the thing you're talking to actually wants. big vs little is not a vibe, it's a fact. πŸ’€

πŸ‘€ SEE ALSO πŸ‘€

  • byteorder(3) β€” yeah it points at its own sibling, that's just how the family is πŸ“

πŸ•°οΈ HISTORY πŸ•°οΈ

The encode/decode functions first appeared in FreeBSD 5.1. so they've been eating since way back, we love a legacy icon πŸ‘‘


and that's a wrap, gorgeous πŸ’– this page is Debian's, dated April 29, 2002 β€” go forth and byte-swap responsibly, mwah 🌈⭐

πŸ“¦ where this page comes from