1、记忆口诀: 小弟弟(小端,低字节 存储在 低地址)
大弟高(大端,低字节 存储在 高地址)
2、大小端判断
typedef union test
{
uint8_t a[2];
short b;
}test_u;
test_u testData;
testData.a[0] = 0x11;
testData.a[1] = 0x22;
//输出 0x2211 是小端; 0x1122 是大端
printf("short B : %#x\r\n", testData.b);
3、大小端转换
typedef unsigned short int uint16;
typedef unsigned long int uint32;
#define BigLittleSwap16(A) ((((uint16)(A) & 0xff00) >> 8) | \
(((uint16)(A) & 0x00ff) << 8))
#define BigLittleSwap32(A) ((((uint32)(A) & 0xff000000) >> 24) | \
(((uint32)(A) & 0x00ff0000) >> 8) | \
(((uint32)(A) & 0x0000ff00) << 8) | \
(((uint32)(A) & 0x000000ff) << 24))