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)