大端,小端又名网络字节序,主机字节序。高位高存,低位低存是小端,复合人们的视觉体验,不用改变位置,如0x0102,在内存的存储位置也是 01 02。而大段相反,复合逻辑,为02 01。如今网络传输,keli5c等使用大端,x86系列使用小段。
unp中有一个程序用来判断大小端:
//endian_check.cpp
#include<iostream>
using namespace std;
int main()
{
union{
short s;
char t[sizeof(s)];
}un;
un.s=0x0102;
if(sizeof(short)==2)
{
if(un.t[0]==1&&un.t[1]==2)
cout<<"big endian"<<endl;
else if(un.t[0]==2&&un.t[1]==1)
cout<<"little endian"<<endl;
else
cout<<"unknow"<<endl;
}
else
cout<<sizeof(short)<<endl;
return 0;
}
一般网络编程使用大小端转换函数htonl,htons,inet_addr,ntohl等。所以你也能明白为何字符串不用转换了罢。