—
NAME
bswap_16, bswap_32, bswap_64 - reverse order of bytes
LIBRARY
Standard C library (libc, -lc)
SYNOPSIS
bash
#include <byteswap.h>bash
uint16_t bswap_16(uint16_t \nx\n);\n
\nuint32_t bswap_32(uint32_t \nx\n);\n
\nuint64_t bswap_64(uint64_t \nx\n);DESCRIPTION
These functions return a value in which the order of the bytes in their 2-, 4-, or 8-byte arguments is reversed.
RETURN VALUE
These functions return the value of their argument with the bytes reversed.
ERRORS
These functions always succeed.
STANDARDS
GNU.
EXAMPLES
The program below swaps the bytes of the 8-byte integer supplied as its command-line argument. The following shell session demonstrates the use of the program:
bash
$ \n./a.out 0x0123456789abcdef\n
0x123456789abcdef ==> 0xefcdab8967452301Program source
bash
#include <byteswap.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char *argv[])
{
\n
uint64_t x;
\n
if (argc != 2) {
\n
fprintf(stderr, "Usage: %s <num>\n", argv[0]);
\n
exit(EXIT_FAILURE);
\n
}
\n
x = strtoull(argv[1], NULL, 0);
\n
printf("%#" PRIx64 " ==> %#" PRIx64 "\n", x, bswap_64(x));
\n
exit(EXIT_SUCCESS);
}SEE ALSO
byteorder(3), endian(3)