SWAP(3)
β¨ 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
- opensuse-leap 16.0: ksh-devel 93vu-bp160.1.33 Β· CPL-1.0
- opensuse-leap 16.0: ksh-devel 93vu-bp160.1.33 Β· CPL-1.0
- opensuse-leap 16.0: ksh-devel 93vu-bp160.1.33 Β· CPL-1.0
- opensuse-leap 16.0: ksh-devel 93vu-bp160.1.33 Β· CPL-1.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms