SWAP(3)

πŸ“¦ version 93vu, in opensuse-leap 16.0

✨ SWAP(3) β€” the integral representation conversion glow-up ✨

okay bestie, gather round, because we're talking about swap today β€” the integral representation conversion routines from the Library Functions Manual, and honestly? underrated. they're the little go-between that translates numbers between internal and external representations so your program can deal with binary data from foreign programs without losing its mind πŸ’…


🌸 SYNOPSIS 🌸

#include <swap.h>

int swapop(const void* internal, const void* external, int width);
int_max swapget(int op, const void* from, int width); void* swapput(int
op, void* to, int width, int_max value); void* swapmem(int op, const
void* from, void* to, size_t n);
note: the last three functions are literally all squished onto those lines in the original synopsis, so we're keeping it exactly like that. character for character, no cap 🫢

πŸ¦‹ DESCRIPTION πŸ¦‹

so these routines convert integral constants between internal and external representations β€” aka they handle the binary data generated by foreign programs. iconic behavior honestly, it's giving "translator for numbers" πŸ”₯

but listen: new binary data representations should use the compact canonical form provided by the sfio(3) routines sfputu and sgetu. that's the assignment, bestie. the swap family is for dealing with the legacy stuff πŸ‘‘


πŸ’œ swapop

swapop returns the swap operation required to convert the width byte integer external to the width byte integer internal.

and the swap operation? it's a bit mask, and it goes a little something like this:

  • 0 β€” no swapping necessary. zero notes, we're chilling 😌
  • 1 β€” swap byte 0 with byte 1.
  • 2 β€” swap bytes 0 and 1 with bytes 2 and 3.
  • 4 β€” swap bytes 0-3 with bytes 4-7, and so on. and so on means and so on, the pattern keeps going, don't @ me πŸ’€

also! the largest native integral type is defined by the macro int_max in the header <int.h>, described over in int(3). keep that in your back pocket ✨


πŸŽ€ swapget

swapget returns the width byte integer in the buffer from, swapped according to op. simple, clean, slays πŸ’–

🧚 swapput

swapput copies the width byte integer value into the buffer to, swapped according to op. and to is returned β€” because we love a return value that actually tells you where things landed 🫢

⭐ swapmem

swapmem swaps n bytes from the buffer from to the buffer to, according to op. and the big mood here: to and from may be the same. yup, in-place is fine, no drama πŸ’…


🌈 SEE ALSO 🌈


that's the whole page, bestie, and it genuinely ate πŸ’€πŸ’– this was SWAP(3) from the Library Functions Manual, and yes, the footer is the same source-and-version-and-date situation as the header β€” SWAP(3), all the way down, bottom of the page, periodt πŸ‘‘βœ¨

now go forth and swap responsibly πŸ¦‹πŸŒΈπŸŽ€

πŸ“¦ where this page comes from