小端字节序
数据低字节位存储在低地址空间。
数据0x12345678的存储方式:
大端字节序
数据高字节位存储在低地址空间。
数据0x12345678的存储方式:
测试系统为大端字节序还是小端字节序
#include <stdio.h>
union Test{
int i;
char c[4];
};
int main()
{
union Test test;
test.i = 0x12345678;
printf("%x",test.c[0]);
return 0;
}
输出与结果:
输出:78 //系统为小端字节序
输出:12 //系统为大端字节序
注意:系统读取数据从低地址开始读取,char[0]为低地址位,如果读取数据为12,而12是数据的高位数据,则高位对应低地址,为大端字节序。