字节序是一个处理器架构特性,用于指示像整数这样的大数据类型内部的字节如何排序。
大端字节序:最大字节地址出现在最低有效字节上。
小端字节序:最低有效字节包含最小字节地址。
#include <iostream>
using namespace std;
static union
{
short s;
char c[sizeof(short)];
} test;
void testBigEndian()
{
test.s=0x0102;
if(test.c[0]==1 && test.c[1]==2)
cout<<"big-endian"<<endl;
else if(test.c[0]==2 && test.c[1]==1)
cout<<"little-endian"<<endl;
else
cout<<"unknown"<<endl;
}
int main(int argc,char **argv)
{
testBigEndian();
return 0;
}
说明:union里面的数据共享同一段内存。