swaw

Copies a specified number of words from a source buffer to a destination buffer, swapping every pair of bytes.

Library:LibC
Classification:Other
Service:Characters and Strings

Syntax

  #include <string.h> 
   
  void swaw (
     const void   *src,
     void         *dst,
     size_t        n);
  

Parameters

src

(IN) Points to the string to be transformed.

dst

(OUT) Points to the array into which to place the transformed characters.

n

(IN) Specifies the number of words to transform.

Remarks

The swaw function copies n words (which should be even) from src to dst swapping every two pairs of characters. This is useful for preparing binary data to be transferred to another machine that has a different byte ordering.

Example

  #include <string.h> 
  #include <stdio.h> 
   
  char *msg = "hTsim seasegi  swspaep.d";  
  #define NBYTES 12  
   
  main()  
  {  
     auto char buffer[80];  
     printf( "%s\n", msg );  
     memset( buffer, `\0’, 80 );  
     swaw( msg, buffer, NBYTES );  
     printf( "%s\n", buffer );  
  }
  

produces the following:

  isThes mgesas  iapswd.pe  
  This message is swapped.