概述:在计算机科学领域中,字节序是指存放多字节数据的字节(Byte)的顺序,考虑单个字节的存储顺序没有意义,典型的应用是整数在内存中的存放方式和在网络中的传输顺序。Endianness有时候也称位序(bit)。
大小端序跟硬件的体系结构有关,所有X86系列的PC机都是小端序,与操作系统无关。在X86的系列的PC上的Solaris系统是小端序,sun sparc平台的Solaris是大端序。
大端序:(Big-Endian)也叫网络字节序,更易读,高位对应低地址
小端序:(Little-Endian)更符合逻辑,高位对应高地址
check_bigendian_littleendian.c
#include <stdio.h>
int main(void)
{
short sh = 0x0102;
if (1 == *(unsigned char *)&sh)
printf(("This is bigendian!\n"));
else if (2 == (*(unsigned char *)&sh))
printf("This is littleendian!\n");
else
printf("Unknown endian!\n");
return 0;
}
比较简单,但是更容易理解,如果有更好的理解方式,欢迎补充!