C Programming/POSIX Reference/unistd.h/swab

From Wikibooks, open books for an open world
Jump to navigation Jump to search

swab is a function in the C programming language that copies one area of memory to another, but swaps adjacent bytes whilst it does so. As far as traceable, “swab” is a contraction of “swap byte”.[citation needed] It is not a standard ISO C library function, but appears in the Single Unix Specification (SUS) and POSIX.1-2001. These require that swab be declared in unistd.h. On other platforms, it may be declared in stdlib.h, if at all.

The swab() function is useful for preparing blocks of data for further processing, in situations where the data blocks have been sent through a network arranged in network byte order, and must be rearranged into native byte order (or vice versa). This deals specifically with data blocks that are composed of 2-byte (16-bit) words that must be rearranged so that they have the proper endianness appropriate for the local system.

External links[edit]