swab -- Swap Adjacent Bytes

Format

#include <stdlib.h>
void swab(char *source, char *destination, int n);

Language Level: Extension
swab copies n bytes from source, swaps each pair of adjacent bytes, and stores the result at destination. The integer n should be an even number to allow for swapping. If n is an odd number, a null character (\0) is added after the last byte.

swab is typically used to prepare binary data for transfer to a machine that uses a different byte order.

Note: In earlier releases of the C/C++ run-time library, swab began with an underscore (_swab). Because it is defined by the X/Open standard, the underscore has been removed. For compatibility, IBM C and C++ Compilers will map _swab to swab for you.

Return Value
There is no return value.

Example
This example copies n bytes from one location to another, swapping each pair of adjacent bytes. In the output x is replaced with null character.

#include <stdlib.h>
#include <stdio.h>
int main(void)
{
   char from[21] = "hTsii  s atsirgn..x ";
   char to[21];
   swab(from, to, 19);  /* swap bytes */
   printf("%s\n", to);
   return 0;
   /****************************************
      The output should be:
      This is a string..
   ****************************************/
}


fgetc -- Read a Character
fputc -- Write Character
<stdlib.h>