大小端数据转换

#define NET_USHORT(u16)   ((unsigned short)((((unsigned short)(u16)) >> 8) | (((unsigned short)(u16)) << 8)))
#define NET_ULONG(u32)   ((unsigned long)(NET_USHORT((u32) >> 16) | (NET_USHORT(u32 & 0xFFFF) << 16)))
#define NET_SHORT(i16)   ((short)NET_USHORT(i16))
#define NET_LONG(i32)   ((long)(NET_USHORT((i32) >> 16) | (NET_USHORT(i32 & 0xFFFF) << 16)))

 

int _tmain(int argc, _TCHAR* argv[])
{
 short a;
 unsigned short b;
 long c;
 unsigned long d;


 unsigned short n = 0x8765;
 printf("n << 8 = 0x%X, n >> 8 = 0x%X/n", (n << 8), (n >> 8));

 printf("n << 8 = 0x%X, n >> 8 = 0x%X/n", (((short)n) << 8), (((short)n) >> 8));

 a = 12;
 a = NET_SHORT(a);
 a = NET_SHORT(a);


 printf("a = %d/n", a);


 b = 0x1234;
 b = NET_USHORT(b);
 b = NET_USHORT(b);

 printf("b = %X/n", b);


 c = 22;
 c = NET_LONG(c);
 c = NET_LONG(c);

 printf("c = %d/n", c);


 d = 0x12345678;
 d = NET_ULONG(d);
 d = NET_ULONG(d);

 printf("d = %X/n", d);
 

 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值