标签:
在网络信息跨主机传输过程中,不同主机的字节序问题可能不同,因此必须进行字节序的转换。
#include<arpa/inet.h>
#include<stdio.h>
//judge host endian
void judge_host_endian()
{
short arg = 0x0102;
short* ap = &arg;
char* temp = (char*)ap;
if(*temp==0x01)
{
puts("host:big-endian");
}
else if(*temp==0x02)
{
puts("host:small-endian");
}
}
//judge net endian
void judge_net_endian()
{
uint16_t arg = htons((uint16_t)0x0102);
short* ap = &arg;
char* temp = (char*)ap;
if(*temp==0x01)
{
puts("net:big-endian");
}
else if(*temp==0x02)
{
puts("net:small-endian");
}
}
int main()
{
judge_host_endian();
judge_net_endian();
return 0;
}
标签:
原文地址:http://www.cnblogs.com/ZhangJinkun/p/4570477.html