1.字节序
当我们与同一台计算机的进程进行通信时,一般不用考虑字节序。什么是字节序——字节序是一个处理器架构特性,用于指示像整数这样的大数据类型内部的字节如何排序。
考虑一个16位整数,它由2个字节组成。内存中存储这两个字节有2种方法:一种是将低序字节存储在起始地址,这称为
小 端字节序;另一种方法 是将高 字节序存储在起始地址,这称为
大端字节序。
1.1 主机字节序
这两种字节序之间没有标准可循,两种格式都有系统使用。有些处理器可以配置成大端的,也可以配置成小端的。我们把某个给定系统所使用的字节序称为
主机字节序。
下面我们给出一个测试程序用来确定我们使用系统的字节序。
#include <iostream>
using namespace std;
int main(void)
{
union
{
short s;
char c[sizeof(short)];
} un;
un.s = 0x0102;
if (sizeof(short) == 2)
{
if (un.c[0] == 1 && un.c[1] == 2)
cout << "big-endian" << endl;
else if (un.c[0] == 2 && un.c[1] == 1)
cout << "little-endian" << endl;
else
cout << "unknown" << endl;
}
else
{
cout << "sizeof(short) = " << sizeof(short) << endl;
}
return 0;
}
1.2 网络字节序
网络协议指定了字节序,因此异构计算机系统能够交换协议信息而不会被字节序所混淆。TCP/IP协议栈使用 大端字节序。应用程序交换格式化数据时,字节序问题就 会出现。对于TCP/IP,地址用网络字节序来表示,所以应用程序有时需要在处理器的字节序与网络字节序之间转换它们。1.3 主机字节序和网络字节序的转换
主机字节序和网络字节序的转换可以使用以下4个函数。
#include <netinet/in.h>
uint16_t htons(uint16_t host16bitvalue);
uint32_t htonl(uint32_t host32bitvalue);
均返回:网络字节序的值
uint16_t ntohs(uint16_t net16bitvalue);
uint32_t ntohl(uint32_t net32bitvalue);
均返回:主机字节序的值
下面是给出的测试代码:
#include <stdio.h>
#include <netinet/in.h>
int main(void)
{
uint32_t uiHost = ntohl(0x01020304); //将主机字节序转换为网络字节序
uint32_t uiNet = htonl(0x01020304); //将网络字节序转换为主机字节序
printf("uiHost = 0x%x, uiNet = 0x%x\n", uiHost, uiNet);
return 0;
}
运行输出结果为:
uiHost = 0x4030201, uiNet = 0x4030201
当使用这些函数时,我们并不关心主机字节序和 网络字节序的真实值(或为大端,或为小端)。我们所要做的只是调用适当的函数在主机和网络字节序之间转换某个给定值。
1.4 glibc字节序
除了使用上面的4个函数之外,我们也能使用glibc提供的字节转换函数。
#include <stdint.h>
#include <endian.h>
uint16_t htobe16(uint16_t host16);
uint32_t htobe32(uint32_t host32);
uint64_t htobe64(uint64_t host64);
均返回:网络字节序的值
uint16_t be16toh(uint16_t net16);
uint32_t be32toh(uint32_t net32);
uint64_t be64toh(uint64_t net64);
均返回:主机字节序的值
下面是给出的测试代码:
#include <stdio.h>
#include <stdint.h>
#include <endian.h>
int main(void)
{
uint32_t uiHost = be32toh(0x01020304);
uint32_t uiNet = htobe32(0x01020304);
printf("uiHost = 0x%x, uiNet = 0x%x\n", uiHost, uiNet);
return 0;
}
运行输出结果:
uiHost = 0x4030201, uiNet = 0x4030201