侧边栏壁纸
博主头像
noerror

虚灵不寐,众理具而万事出。

  • 累计撰写 239 篇文章
  • 累计创建 9 个标签
  • 累计收到 2 条评论
标签搜索

目 录CONTENT

文章目录

bswap函数用法详解

noerror
2022-10-04 / 0 评论 / 0 点赞 / 206 阅读 / 295 字 / 正在检测是否收录...

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);
}
0

评论区