wow-byte字节操作说明

这篇文章介绍了wow-byte文件中关于数据类型(如uint32_t)在不同字节序(小端le、大端be和默认ne)下的处理,包括获取和设置宏定义的详细示例代码。
摘要由CSDN通过智能技术生成

wow-byte文件说明

  • 项目地址:https://gitee.com/wow-iot/wow-iot7
  • 本文件的初衷是在面临数据类型大小端时,更简洁的处理各种数据类型
    下面给出uint32_t类型实例,其中le-小端数据 be-大端数据 ne-默认直接转换。
    其它具体实现可直接参考wow_type.h实现;

获取语句宏定义

#define byte_get_32_le_impl(p)  ((uint32_t)(((uint8_t)*((p) + 3)) << 24 | ((uint8_t)*((p) + 2)) << 16 | ((uint8_t)*((p) + 1)) << 8 | ((uint8_t)*(p))))
#define byte_get_32_be_impl(p)  ((uint32_t)(((uint8_t)*(p)) << 24 | ((uint8_t)*((p) + 1)) << 16 | ((uint8_t)*((p) + 2)) << 8 | ((uint8_t)*((p) + 3))))
#define byte_get_32_ne_impl(p)  (*((uint32_t*)(p)))
设置语句宏定义:
static __inline__ void byte_set_u32_le(uint8_t* p, uint32_t x)
{ 
	p[0] = (uint8_t)x&0xFF;
	p[1] = (uint8_t)(x >> 8)&0xFF; 
	p[2] = (uint8_t)(x >> 16)&0xFF;
	p[3] = (uint8_t)(x >> 24)&0xFF;
}
static __inline__ void byte_set_u32_be(uint8_t* p, uint32_t x)
{
	p[0] = (uint8_t)(x >> 24)&0xFF; 
	p[1] = (uint8_t)(x >> 16)&0xFF; 
	p[2] = (uint8_t)(x >> 8)&0xFF; 
	p[3] = (uint8_t)x;
}


#define byte_set_32_le_impl(p, x)       byte_set_u32_le(p, x)
#define byte_set_32_be_impl(p, x)       byte_set_u32_be(p, x)
#define byte_set_u32_ne_impl(p, x)      do { *((uint32_t*)(p)) = (uint32_t)(x); } while (0)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值