Edit

Share via


_swab

Swaps bytes.

Syntax

void _swab(
   char *src,
   char *dest,
   int n
);

Parameters

src
Data to be copied and swapped.

dest
Storage location for swapped data.

n
Number of bytes to be copied and swapped.

Return value

The swab function doesn't return a value. The function sets errno to EINVAL if either the src or dest pointer is null or n is less than zero, and the invalid parameter handler is invoked, as described in Parameter validation.

For more information about return codes, see errno, _doserrno, _sys_errlist, and _sys_nerr.

Remarks

If n is even, the _swab function copies n bytes from src, swaps each pair of adjacent bytes, and stores the result at dest. If n is odd, _swab copies and swaps the first n-1 bytes of src, and the final byte isn't copied. The _swab function is typically used to prepare binary data for transfer to a machine that uses a different byte order.

By default, this function's global state is scoped to the application. To change this behavior, see Global state in the CRT.

Requirements

Routine Required header
_swab C: <stdlib.h> C++: <cstdlib> or <stdlib.h>

For more compatibility information, see Compatibility.

Example

// crt_swab.c

#include <stdlib.h>
#include <stdio.h>

char from[] = "BADCFEHGJILKNMPORQTSVUXWZY";
char to[] =   "...........................";

int main()
{
    printf("Before: %s  %d bytes\n        %s\n\n", from, sizeof(from), to);
    _swab(from, to, sizeof(from));
    printf("After:  %s\n        %s\n\n", from, to);
}
Before: BADCFEHGJILKNMPORQTSVUXWZY  27 bytes
        ...........................

After:  BADCFEHGJILKNMPORQTSVUXWZY
        ABCDEFGHIJKLMNOPQRSTUVWXYZ.

See also

Buffer manipulation