字节序判断

版权声明:仅限学习使用 https://blog.csdn.net/u014590889/article/details/89379362
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<arpa/inet.h>

typedef union
{
    unsigned short int val;
    unsigned char      byte[2];
}to;
/*
小端字节序
[[email protected] /newhome/wuyaoyao/Linux_Bash/tree]# ./avltree
 test.byte[0]= 0xcd; test.byte[1] = 0xab
*/
void judge()
{
    to test;
    test.val = 0xabcd;
    
    printf(" test.val    = 0x%x\n", test.val);
    printf(" test.byte[0]= 0x%x; test.byte[1] = 0x%x\n", test.byte[0], test.byte[1]);
    
    test.val = htons(test.val);
    printf(" test.byte[0]= 0x%x; test.byte[1] = 0x%x\n", test.byte[0], test.byte[1]);
    
    test.val = ntohs(test.val);
    printf(" test.byte[0]= 0x%x; test.byte[1] = 0x%x\n", test.byte[0], test.byte[1]);
    
    test.val = htonl(test.val);
    printf(" test.byte[0]= 0x%x; test.byte[1] = 0x%x\n", test.byte[0], test.byte[1]);
}

void main()
{
    judge();

    return;
}
[[email protected] /newhome/wuyaoyao/Linux_Bash/tree]# gcc -o avltree avltree.c
[[email protected] /newhome/wuyaoyao/Linux_Bash/tree]# ./avltree
 test.val    = 0xabcd
 test.byte[0]= 0xcd; test.byte[1] = 0xab
 test.byte[0]= 0xab; test.byte[1] = 0xcd
 test.byte[0]= 0xcd; test.byte[1] = 0xab
 test.byte[0]= 0x0; test.byte[1] = 0x0

猜你喜欢

转载自blog.csdn.net/u014590889/article/details/89379362