guanpengchn / guanpengchn.github.io

:memo: code on DEV branch, blogs on ISSUES
https://guanpengchn.github.io
18 stars 9 forks source link

大小端判断和转换 #37

Open guanpengchn opened 5 years ago

guanpengchn commented 5 years ago
BOOL IsBigEndian()  
{  
    int a = 0x1234;  
    char b =  *(char *)&a;  //通过将int强制类型转换成char单字节,通过判断起始存储位置。即等于 取b等于a的低地址部分  
    if( b == 0x12)  
    {  
        return TRUE;  
    }  
    return FALSE;  
}

#define BigtoLittle32(A)   ((( (uint32)(A) & 0xff000000) >> 24) | \  
                                       (( (uint32)(A) & 0x00ff0000) >> 8)   | \  
                                       (( (uint32)(A) & 0x0000ff00) << 8)   | \  
                                       (( (uint32)(A) & 0x000000ff) << 24))