大小端判断

#include "unp.h"
int main(int argc, char **argv)
{
    printf("%s\n", CPU_VENDOR_OS);
    union{
        short s;
        char c[sizeof(short)];
    }un;
    un.s = 0x0102;
    if (2 == un.c[0] && 1 == un.c[1])
        printf("Little-endian\n");
    else
        printf("Big-endian\n");
    return 0;
}

 网络字节序通常为大端。

主机字节序和网络字节序相互转换:htons() htonl() ntohs() ntohl()

posted @ 2016-04-18 19:41  LarryKnight  阅读(146)  评论(0编辑  收藏  举报