_swab

 

The latest version of this topic can be found at _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 does not 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.

See _doserrno, errno, _sys_errlist, and _sys_nerr for more information on this and other return codes.

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 is not copied. The _swab function is typically used to prepare binary data for transfer to a machine that uses a different byte order.

Requirements

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

For additional compatibility information, see Compatibility in the Introduction.

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);  
}```  
  
```Output  
Before: BADCFEHGJILKNMPORQTSVUXWZY  27 bytes  
        ...........................  
  
After:  BADCFEHGJILKNMPORQTSVUXWZY  
        ABCDEFGHIJKLMNOPQRSTUVWXYZ.  

See Also

Buffer Manipulation