bswap函数用法详解
bswap函数简介
- 头文件包含
#include <byteswap.h>
- 函数定义
bswap_16( x );
bswap_32( x );
bswap_64( x );
bswap函数常见使用错误
- 编译错误
warning: implicit declaration of function ‘bswap’ [-Wimplicit-function-declaration]
解决办法:包含头文件
#include <byteswap.h>
bswap函数详细描述
这些宏返回一个值,在该值中,它们的2字节、4字节或8字节参数中的字节顺序是颠倒的。
bswap函数返回值
这些宏返回参数的值,并将字节反转。
bswap函数错误码
这些宏总是成功的。
bswap函数使用举例
下面的程序交换作为命令行参数提供的8字节整数的字节。下面的shell会话演示该程序的用法:
$ \fB./a.out 0x0123456789abcdef\fP
0x123456789abcdef ==> 0xefcdab8967452301
Program source&
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <inttypes.h>
#include <byteswap.h>
int
main(int argc, char *argv[])
{
uint64_t x;
if (argc != 2) {
fprintf(stderr, "Usage: %s <num>\en", argv[0]);
exit(EXIT_FAILURE);
}
x = strtoull(argv[1], NULL, 0);
printf("%#" PRIx64 " ==> %#" PRIx64 "\en", x, bswap_64(x));
exit(EXIT_SUCCESS);
}
评论区