原文地址:https://blog.csdn.net/jk110333/article/details/7872695

小心不要假设字节序. PC 存储多字节值是低字节为先(小端为先, 因此是小端), 一些高级的平台以另一种方式(大端)工作. 任何可能的时候, 你的代码应当这样来编写, 它不在乎它操作的数据的字节序. 但是, 有时候一个驱动需要使用单个字节建立一个整型数或者相反, 或者它必须与一个要求一个特定顺序的设备通讯.

包含文件 <asm/byteorder.h> 定义了或者 __BIG_ENDIAN 或者 __LITTLE_ENDIAN, 依赖处理器的字节序. 当处理字节序问题时, 你可能编码一堆 #ifdef __LITTTLE_ENDIAN 条件语句, 但是有一个更好的方法. Linux 内核定义了一套宏定义来处理之间的转换, 在处理器字节序和你需要以特定字节序存储和加载的数据之间. 例如:

u32 cpu_to_le32 (u32);
u32 le32_to_cpu (u32);

这 2 个宏定义转换一个值, 从无论 CPU 使用的什么到一个无符号的, 小端, 32 位数, 并且转换回. 它们不管你的 CPU 是小端还是大端, 不管它是不是 32-位 处理器. 在没有事情要做的情况下它们原样返回它们的参数. 使用这些宏定义易于编写可移植的代码, 而不必使用大量的条件编译建造.

有很多类似的函数; 你可以在 <linux/byteorder/big_endian.h> 和 <linux/byteorder/little_endian.h> 中见到完整列表. 一会儿之后, 这个模式不难遵循. be64_to_cpu 转换一个无符号的, 大端, 64-位 值到一个内部 CPU 表示. le16_to_cpus, 相反, 处理有符号的, 小端, 16 位数. 当处理指针时, 你也会使用如 cpu_to_le32p, 它使用指向一个值的指针来转换, 而不是这个值自身. 剩下的看包含文件.

Then when do we use le32_to_cpu( ) , and when do we use cpu_to_le32( )? 

> The names tell it.

> le32_to_cpu is used for convesions from 32bit little endian data into CPUs endianness

> cpu_to_le32 is used for convesions from CPU endianness to little endian 32bit data. 
> Actually, both macros do the same thing, but one should make the differences clear to make code more readable so that anyone can quickly find out whether some data is kept in native endianness or some particular one.

可以使用下面的方法来判断处理器使用的什么模式

int GetEndianness()
{short s = 0x0110;char *p = (char *) &s;if (p[0] == 0x10)return 0;// 小端格式elsereturn 1;// 大端格式
}

参考:http://blog.chinaunix.net/uid-15751163-id-2762241.html

代码中le32_to_cpu()、le16_to_cpu()按具体CPU的要求进行数据的排列,在i386处理器上访问Ext2文件系统时这些函数不做任何事情。因为不同的处理器在存取数据时在字节的排列次序上有所谓“big ending”和“little ending”之分。例如,i386就是“little ending”处理器,它在存储一个16位数据0x1234时,实际存储的却是0x3412,对32位数据也是如此。这里索引节点号与块的长度都作为32位或16位无符号整数存储在磁盘上,而同一磁盘既可以安装在采用“little ending”方式的CPU机器上,也可能安装在采用“big ending”方式的CPU机器上,所以要选择一种形式作为标准。事实上,Ext2采用的标准为“little ending”,所以,le32_to_cpu()、le16_to_cpu()函数不作任何转换。

BSD Kernel Developer's Manual
NAMEbswap16, bswap32, bswap64, be16toh, be32toh, be64toh, htobe16, htobe32,htobe64, htole16, htole32, htole64, le16toh, le32toh, le64toh, be16enc,be16dec, be32enc, be32dec, be64enc, be64dec, le16enc, le16dec, le32enc,le32dec, le64enc, le64dec -- byte order operationsSYNOPSIS#include <sys/endian.h>uint16_tbswap16(uint16_t int16);uint32_tbswap32(uint32_t int32);uint64_tbswap64(uint64_t int64);uint16_tbe16toh(uint16_t big16);uint32_tbe32toh(uint32_t big32);uint64_tbe64toh(uint64_t big64);uint16_thtobe16(uint16_t host16);uint32_thtobe32(uint32_t host32);uint64_thtobe64(uint64_t host64);uint16_thtole16(uint16_t host16);uint32_thtole32(uint32_t host32);uint64_thtole64(uint64_t host64);uint16_tle16toh(uint16_t little16);uint32_tle32toh(uint32_t little32);uint64_tle64toh(uint64_t little64);uint16_tbe16dec(const void *);uint32_tbe32dec(const void *);uint64_tbe64dec(const void *);uint16_tle16dec(const void *);uint32_tle32dec(const void *);uint64_tle64dec(const void *);voidbe16enc(void *, uint16_t);voidbe32enc(void *, uint32_t);voidbe64enc(void *, uint64_t);voidle16enc(void *, uint16_t);voidle32enc(void *, uint32_t);voidle64enc(void *, uint64_t);DESCRIPTIONThe bswap16(), bswap32(), and bswap64() functions return a byte orderswapped integer.  On big endian systems, the number is converted to lit-tle endian byte order.  On little endian systems, the number is convertedto big endian byte order.The be16toh(), be32toh(), and be64toh() functions return a big endianbyte ordered integer converted to the system's native byte order.   Thereturn value will be the same as the argument on big endian systems.The le16toh(), le32toh(), and le64toh() functions return a little endianbyte ordered integer converted to the system's native byte order.   Thereturn value will be the same as the argument on little endian systems.The htobe16(), htobe32(), and htobe64() functions return a integer in thesystem's native byte order converted to big endian byte order.  Thereturn value will be the same as the argument on big endian systems.The htole16(), htole32(), and htole64() functions return a integer in thesystem's native byte order converted to little endian byte order.  Thereturn value will be the same as the argument on little endian systems.The be16enc(), be16dec(), be32enc(), be32dec(), be64enc(), be64dec(),le16enc(), le16dec(), le32enc(), le32dec(), le64enc(), and le64dec()functions encode and decode integers to/from byte strings on any align-ment in big/little endian format.SEE ALSObyteorder(3)HISTORYThe hto*() and toh*() functions first appeared in FreeBSD 5.0, and wereoriginally developed by the NetBSD project.The encode/decode functions first appeared in FreeBSD 5.1.BSD                April 29, 2002                 BSD

Linux 大小端转换函数相关推荐

  1. Linux大小端转换实现

    实现 #include <byteswap.h> #include <stdint.h>/*** @brief 8字节类型的字节序转化*/ template<class ...

  2. honts/htonl等大小端转换函数的原理

    之前在写网络程序时,基本都会发出去之前调用htons/htonl.接收端收到报文后按照ntohs/ntohl来反解析,只知道是为了处理不同机器架构大小端带来的问题,并未深入思考其中的逻辑,今天突然想到 ...

  3. 使用函数实现数据大小端转换

    使用函数实现数据大小端转换 题目描述 在数字芯片设计中,经常把实现特定功能的模块编写成函数,在需要的时候再在主模块中调用,以提高代码的复用性和提高设计的层次,分别后续的修改. 请用函数实现一个4bit ...

  4. TCP/IP,网络字节序与本地转换,Linux提供的转换函数htonl,htons等,sock_addr的数据结构与内容

    TCP/IP,网络字节序与本地转换,Linux提供的转换函数htonl,htons等,sock_addr的数据结构与内容 一.网络字节序存储方式: 小端存储:高位存放高地址,0x12345678的小段 ...

  5. Verilog学习之数据大小端转换设计

    文章目录 前言 一.题目描述 二.实现思路 1.函数的概念 2.具体思路 三.代码展示 总结 前言 今天我们做的是第十道题--使用函数实现数据大小端转换,这道题其实也比较简单,就是逆向输出一个数据,但 ...

  6. c语言高低位拷贝_C语言中的大小端转换与高低位颠倒

    总结一下最近工作学到的东西,主要是关于大小端与高低位. 在说大小端高低位之前,肯定要说明数据在计算机内是如何存储的.在计算机中,我们将数据分割成了一个一个的字节(byte),而每个字节又有8位(bit ...

  7. 来点基础的东西,关于浮点数的大小端转换以及浮点数的格式解析

    在网上并没有任何信息或则资料介绍关于浮点数的大小端转换的原理的问题,大小端是不同的内存存储实现方式,大端更符合人的阅读习惯,而小端则是更适合CPU读取. 我先说整型,来说明大小端在内存中的存储方式,整 ...

  8. C语言中的大小端转换与高低位颠倒

    在说大小端高低位之前,肯定要说明数据在计算机内是如何存储的.在计算机中,我们将数据分割成了一个一个的字节(byte),而每个字节又有8位(bit). 一个字节,可以声明为unsigned char型数 ...

  9. java大小端转换工具类

    最近的项目频繁涉及大小端转换的情况,参考github(https://github.com/Frank-Wiebeler/java-Big2LittleEndian/blob/master/src/c ...

  10. Java 大小端转换(基于ByteBuffer)

    大小端的基础知识: 小端 ( little-endian):低位字节在前,高位字节在后.大端(Big-Endian),则反之.具体而言,就是为了说清楚,CPU架构中1字(word)的存储顺序.计算机内 ...

最新文章

  1. 转:python——IPy库
  2. 算法-排序-基数排序(对任意整数排序)
  3. 推荐系统——矩阵分解FM
  4. Android未发现目标设备,Android设备不会显示为Unity3d调试的目标
  5. CCPC-Wannafly Comet OJ 夏季欢乐赛(2019)E
  6. PHP上传大文件 分割文件上传
  7. [51nod1116]K进制下的大数
  8. mysql tee_MySQL 使用tee记录语句和输出日志
  9. java.lang.Classlt;Tgt;
  10. python random random_【python】random与numpy.random
  11. Opencv3.4.2调用yolov2进行物体检测源代码
  12. DateUtils工具类
  13. 分布式数据库实战第三节 分布式数据库引擎、索引和事务
  14. lgv50进入工程模式_LG手机工程模式进入方法及菜单指令翻译(适用G6、G7、V20、V30等)...
  15. 【使用QGIS入库将shp数据导入postgis、postgres数据库】
  16. win7安装高版本的node解决办法
  17. 生产者消费者模型详解以及实现
  18. Java 窗口透明化(无边框)
  19. 海康威视iSC 平台第三方对接门禁权限分享
  20. 去污染(宿主)过程记录

热门文章

  1. 单片机 STM32 HAL 闪存 AT24C02
  2. 单片机编程软件很简单(12),Keil单片机编程软件配置操作
  3. C# 实现简易的串口监视上位机功能附源码下载
  4. 手机上最好用的五笔输入法_手机输入法之争:九宫格和全键盘到底哪个更好用...
  5. java 图片处理工具类(图片简单处理 java原生)
  6. Android下的默认字体详解
  7. AdobeFlashPlayer:GPU加速原理解析
  8. 计算机组成原理-王道习题1
  9. 【项目篇-软件项目技术方案怎么写?(五千字图文总结建议)】软件平台类创新创业竞赛项目计划书、新苗国创(大创)申报书
  10. 打造黑苹果(一)组装硬件的选择与组装