swap adjacent bytes

View versions (3)

INTERFACE

#include <string.h>
void swab ( const void * restrict src, void * restrict dst, size_t len )

DESCRIPTION

The function swab copies len bytes from the location referenced by src to the location referenced by dst, swapping adjacent bytes. This is used to copy data from one machine to another with a different byte order.

The argument len must be an even number.

Example 1
Workings
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main()
{
  const char* source="oCedoCsgi  srgae t";
  char target[18];
  swab(source, target, strlen(source));
  printf("swab(%s) becomes (%s)",source, target);
  return 0; 
}
Solution

Output:

swab(oCedoCsgi  srgae t) becomes (CodeCogs is great )

SEE ALSO

memset

HISTORY

A swab function appeared in Version 7 AT&T UNIX.