字节排序函数
来源:互联网 发布:水电安装 知乎 编辑:程序博客网 时间:2024/06/05 04:58
Consider a 16-bit integer that is made up of 2 bytes. There are two ways to store the twobytes in memory: with the low-order byte at the starting address, known as little-endian byteorder, or with the high-order byte at the starting address, known as big-endian byte order
1. 大端字节序(Big Endian)
最高有效位(MSB:Most Significant Bit)存储于最低内存地址处;
最低有效位(LSB:Lowest Significant Bit)存储于最高内存地址处。
2. 小端字节序(Little Endian)最高有效位(MSB:Most Significant Bit)存储于最高内存地址处;
最低有效位(LSB:Lowest Significant Bit)存储于最低内存地址处。
主机字节序
不同的主机有不同的字节序,如x86为小端字节序,Motorola 6800为大端字节序,ARM字节序是可配置的。
网络字节序
网络字节顺序是TCP/IP中规定好的一种数据表示格式,它与具体的CPU类型、操作系统等无关,从而可以保证数据在不同主机之间传输时能够被正确解释。网络字节顺序采用big endian排序方式。
为使网络程序具有可移植性,使同样的C代码在大端和小端计算机上编译后都能正常运行,可以调用以下库函数做网络字节序和主机字节序的转换。
#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);
这些函数名很好记:
- h表示host
- n表示network
- l表示32位长整数
- s表示16位短整数。
例如htonl表示将32位的长整数从主机字节序转换为网络字节序,例如将IP地址转换后准备发送。如果主机是小端字节序,这些函数将参数做相应的大小端转换然后返回,如果主机是大端字节序,这些函数不做转换,将参数原封不动地返回。
《UNIX网络编程 卷1:套接字联网API(第3版)》中检查主机的大端小端程序如下:
#include "unp.h"int main(int argc, char **argv){ union { short s; char c[sizeof(short)]; } un; un.s = 0x0102; //短整数变量中存放2个字节的值0x0102 printf("%s: ", CPU_VENDOR_OS); //标识CPU类型,厂家和操作系统版本,如:i386-apple-darwin14.5.0 if(sizeof(short) == 2) { //查看它的两个连续字节c[0],c[1]来确定字节序 if (un.c[0] == 1 && un.c[1] == 2) printf("big-endian\n"); else if (un.c[0] == 2 && un.c[1] == 1) printf("litter-endian\n"); else printf("unknown\n"); } else printf("sizeof(short) = %lu\n", sizeof(short)); exit(0);}
还有一个更加简明的方式,如下:
#include <stdio.h>#include <arpa/inet.h>int main(void){ unsigned int x = 0x12345678; unsigned char *p = (unsigned char *) &x; printf("%x,%x,%x,%x\n", p[0], p[1], p[2], p[3]); unsigned int y = htonl(x); p = (unsigned char *)&y; printf("%x,%x,%x,%x\n", p[0], p[1], p[2], p[3]); return 0;}
输出:
78 56 34 12 12 34 56 78
即本主机是小端字节序,而经过htonl转换后为网络字节序,即大端。
参考:
1.《UNIX网络编程 卷1:套接字联网API(第3版)》
2. UNIX网络编程——socket概述和字节序、地址转换函数
阅读全文
0 0
- 字节排序函数
- 字节排序函数
- 字节排序函数
- 字节排序函数与字节操纵函数
- 《ASCE1885的网络编程》---Winsock APIの字节排序函数
- Windows Sockets:字节排序
- 字节排序问题
- Windows Sockets:字节排序
- UNIX网络编程卷1(第三版)字节排序函数测试
- Unix网络编程(二):字节排序与地址转换函数
- 计算字节大小函数
- 字节处理函数
- 字节序处理函数
- 字节序转换函数
- ASM字节填充函数
- 字节操纵函数
- 字节次序转换函数
- PHP字节转换函数
- Linux 第n次驱动入门(一) ---- helloWorld
- Android 状态栏颜色设置
- Android 8.0 Oreo 现已推出!
- Java web 中几个常用的获取项目路径的方法
- 我为什么建议自建HBase集群应该迁移过来?
- 字节排序函数
- mysql 的like 和正则表达式 regexp
- 在局域网下怎样控制另一台电脑
- tensorflow|函数讲解
- CodeForces
- Jimmy 的文档: 单机版openshift origin-1.5.0安装配置和简单实例
- SpringMVC的视图和视图解析器
- 数据结构实验之查找二:平衡二叉树
- (POJ