Endian
Endian
寻址
多字节对象被存储为连续的字节序列,对象的地址为所使用字节中最小的地址。
例如,假设一个类型为 int 的变量 a 的地址为 0x100,也就是说,地址表达式 &a 的值为 0x100。那么,(假设数据类型 int 为32位表示) a 的 4 个字节将被存储在内存的 0x100、0x101、0x102 和 0x103 位置。
字节顺序
最低有效字节存储在起始地址,这称为小端(Little Endian)字节序;最高有效字节存储在起始地址,这称为大端(Big Endian)字节序。
术语“小端”和“大端”表示多字节值的哪一端(小端或大端)存储在该值的起始地址。
大多数 Intel 兼容机都只用小端模式。另一方面,IBM 和 Oracle 的大多数机器则是按大端模式操作。
在下图中,我们标明内存地址增长的方向为从左到右。我们还标明最高有效位(Most Significant Bit,MSB)是这个32位值最左边一位,最低有效位(Least Significant Bit,LSB)是这个32位值最右边一位。 最高有效字节(Most Significant Byte)是 Byte3=0x01,最低有效字节(Least Significant Byte)是 Byte0=0x67 。


Endian Conversion Functions
我们把某个给定系统所用的字节序称为主机字节序(host byte order),网络协议为网络字节序(network byte order)(大端字节序)。
网络字节序是大端字节序。
Linux
下面4个函数是主机字节序和网络字节序之间相互转换的函数。在这些函数的名字中,h代表host,n代表network,s代表short,l代表long。short和long这两个称谓是出自4.2BSD的Digital VAX实现的历史产物。如今我们应该把s视为一个16位的值(例如TCP或UDP端口号),把l视为一个32位的值(例如IPv4地址)。事实上即使在64位的Digital Alpha中,尽管长整数占用64位,htonl和ntohl函数操作的仍然是32位的值。
当使用这些函数时,我们并不关心主机字节序和网络字节序的真实值(或为大端,或为小端)。我们所要做的只是调用适当的函数在主机和网络字节序之间转换某个给定值。在那些与网际协议所用字节序(大端)相同的系统中,这四个函数通常被定义为空宏。
// converts host byte order and network byte order
#include <arpa/inet.h>
uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostshort);
uint32_t ntohl(uint32_t netlong);
uint16_t ntohs(uint16_t netshort);
These functions convert the byte encoding of integer values from the byte order that the current CPU(the "host") uses, to and from little-endian and big-endian byte order.
// These functions convert the byte encoding of integer values from the byte order that the current CPU (the "host") uses,
// to and from little-endian and big-endian byte order.
#include <endian.h>
uint16_t htobe16(uint16_t host_16bits);
uint16_t htole16(uint16_t host_16bits);
uint16_t be16toh(uint16_t big_endian_16bits);
uint16_t le16toh(uint16_t little_endian_16bits);
uint32_t htobe32(uint32_t host_32bits);
uint32_t htole32(uint32_t host_32bits);
uint32_t be32toh(uint32_t big_endian_32bits);
uint32_t le32toh(uint32_t little_endian_32bits);
uint64_t htobe64(uint64_t host_64bits);
uint64_t htole64(uint64_t host_64bits);
uint64_t be64toh(uint64_t big_endian_64bits);
uint64_t le64toh(uint64_t little_endian_64bits);
Boost
Boost.Endian: The Boost Endian Library - 1.77.0
References
byteorder(3) - Linux manual page (man7.org)
Endian的更多相关文章
- unicode,ansi,utf-8,unicode big endian编码的区别
知乎--http://www.zhihu.com/question/23374078 http://wenku.baidu.com/view/cb9fe505cc17552707220865.html ...
- [转]unicode,ansi,utf-8,unicode big endian的故事
unicode,ansi,utf-8,unicode big endian的故事很久很久以前,有一群人,他们决定用8个可以开合的晶体管来组合成不同的状态,以表示世界上的万物.他们看到8个开关状态是好的 ...
- c#,关于Big Endian 和 Little Endian,以及转换类
Big Endian:最高字节在地址最低位,最低字节在地址最高位,依次排列. Little Endian:最低字节在最低位,最高字节在最高位,反序排列. 当在本地主机上,无需注意机器用的是Big En ...
- 大端(big endian)和小端(little endian)
http://www.cnblogs.com/Romi/archive/2012/01/10/2318551.html 当前的存储器,多以byte为访问的最小单元,当一个逻辑上的地址必须分割为物理上的 ...
- sizeof usage & big / little endian
http://blog.csdn.net/w57w57w57/article/details/6626840 http://people.cs.umass.edu/~verts/cs32/endian ...
- <QtEndian> - Endian Conversion Functions
The <QtEndian> header provides functions to convert between little and big endian representati ...
- Check Big/Little Endian
Little endian:Low memory address stores low byte value.(eg. short int 0x2211 0xbfd05c0e->0x11 ...
- 字符编码笔记:ASCII,Unicode和UTF-8,附带 Little endian和Big endian的解释
作者: 阮一峰 日期: 2007年10月28日 今天中午,我突然想搞清楚Unicode和UTF-8之间的关系,于是就开始在网上查资料. 结果,这个问题比我想象的复杂,从午饭后一直看到晚上9点,才算初步 ...
- 字符编码笔记:ASCII、Unicode、UTF-8、UTF-16、UCS、BOM、Endian
转载:http://witmax.cn/character-encoding-notes.html 今天中午,我突然想搞清楚Unicode和UTF-8之间的关系,于是就开始在网上查资料. 结果,这个问 ...
- write a macro to judge big endian or little endian
Big endian means the most significant byte stores first in memory. int a=0x01020304, if the cpu is b ...
随机推荐
- 用GUI实现java版贪吃蛇小游戏
项目结构 新建一个JFrame窗口,作为程序入口 public class GameStart{ public static void main(String[] args) { JFrame jFr ...
- GUI编程路线
基本路线
- Linux proc文件系统小记
序言: 当linux系统存在多个网口时(也可以是一个网口配置两个IP),每个网口配置不同的IP地址,并且分别连接到不同的电脑上,电脑分别配置与相连的网口统一网段的IP且将电脑网关设置为linux的网口 ...
- 手工添加数据到Chart
private void button1_Click(object sender, EventArgs e) { //清除Series chart1.Series.Clear(); Random ra ...
- AspectJ——AOP框架快速入门
一.导包 二.bean.xml配置 三.环绕通知 四,表达式
- golang指针接收者和值接收者方法调用笔记
初学go时很多同学会把 值接收者 和 指针接收者 的方法相互调用搞混淆,好多同学都只记得指针类型可以调用值接收者方法和指针接收者方法,而值类型只能调用值接收者方法,其实不然,在某些情况下,值类型也是可 ...
- angularjs $http.get 和 $http.post 传递参数
$http.get请求数据的格式 $http.get(URL,{ params: { "id":id } }) .success(function(response, status ...
- python面向对象(封装,继承,多态)
python面向对象(封装,继承,多态) 学习完本篇,你将会深入掌握 如何封装一个优雅的借口 python是如何实现继承 python的多态 封装 含义: 1.把对象的属性和方法结合成一个独立的单位, ...
- python函数模块
python函数模块 学习完本篇,你将会深入掌握 函数相关知识 1.函数定义 2.函数如何调用 3.递归函数以及匿名函数使用 模块相关知识 1.模块如何导入调用 2.函数调用顺序 函数定义 函数代码块 ...
- VBox 虚拟机安装 Openwrt 做旁路由
VBox 虚拟机安装 Openwrt 做旁路由 需求:开个虚拟机做旁路由,电脑把网关设置成旁路由地址,用它跑个上网或其他什么东西. 安装及配置过程简述 这件事流程很简单,总结起来主要有以下几点: 安装 ...