拆分
//16位拆成两个8位
u16 data16 = 0x1234;
u8 data8_H,data8_L;
data8_H = (u8)(data16 >> 8);
data8_L = (u8)data16;
32位拆成4个8位
办法一:
u32 data32 = 0x12345678;
u8 data8_1,data8_2,data8_3,data8_4;
data8_1 = (u8)(data32 >> 24);
data8_2 = (u8)(data32 >> 16);
data8_3 = (u8)(data32 >> 8);
data8_4 = (u8)data32;
办法二:
u32 data32 = 0x12345678;
u8 data8_1,data8_2,data8_3,data8_4;
u8 *pa; //界说指针
pa = (u8 *)(&data32); //将32位地址,强制转换为8位地址
data8_1 = *((char *)pa+3);
data8_2 = *((char *)pa+2);
data8_3 = *((char *)pa+1);
data8_4 = *((char *)pa);
兼并
2个8位兼并成1个16位
u8 data8_H = 0x12;
u8 data8_L = 0x34;
u16 data16;
data16 = ((u16)data8_H << 8) | (u16)(data8_L));
4个8位兼并成1个32位
u8 data8_1 = 0x12;
u8 data8_2 = 0x34;
u8 data8_3 = 0x56;
u8 data8_4 = 0x78;
u32 data32;
data32 = ((u32)data8_1 << 24) | ((u32)data8_2 << 16) | ((u32)data8_3 << 8) | ((u32)data8_4);