大端小端转换

大小端转换方法:

#include

#include

#define Tranverse16(X)                 ((((UINT16)(X) & 0xff00) >> 8) |(((UINT16)(X) & 0x00ff) << 8))
#define Tranverse32(X)                 ((((UInt32)(X) & 0xff000000) >> 24) | /
                                                           (((UInt32)(X) & 0x00ff0000) >> 8) | /
                                                           (((UInt32)(X) & 0x0000ff00) << 8) | /
                                                           (((UInt32)(X) & 0x000000ff) << 24))

void main()
{


 //测试本地是大端还是小端

//取出一个四字节的最低一个字节,若取出的最低位与原来整数的最低位一致,说明是小端

  int i = 1;

 //if (*(char*)&i==1)

//{
 // printf("小端");

//}

 //else

//{

    printf("大端");

//}
 UINT16 data = Tranverse16(256);//data =1
 UINT32 data1 = Tranverse16(1);// data1 = 256

}

你可能感兴趣的:(大端小端转换)