大小端转换

来源:互联网 发布:sql查询去掉重复记录 编辑:程序博客网 时间:2024/06/05 21:14
#include <stdio.h>typedef unsigned short __u16;typedef unsigned int __u32;#define ___swab16(__x)  ((__u16)((((__u16)(__x) & (__u16)0x00ffU) << 8) |(((__u16)(__x) & (__u16)0xff00U) >> 8) ))#define ___swab32(__x) ((__u32)((((__u32)(__x) & (__u32)0x000000ffUL) << 24) | \                             (((__u32)(__x) & (__u32)0x0000ff00UL) << 8) | \                             (((__u32)(__x) & (__u32)0x00ff0000UL) >> 8) | \                             (((__u32)(__x) & (__u32)0xff000000UL) >> 24) ))int main(void){    __u16 a = 0xabcd;    __u32 b = 0x12345678;    __u32 c = 0x78563412;    printf("0x%x\n",___swab16(a));    printf("0x%x\n",___swab32(b));    printf("0x%x\n",___swab32(c));    return 0;}

原创粉丝点击