
#include<arpa/inet.h>#include<stdio.h>//judge host endianvoid 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 endianvoid 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