1.8位数据转16位数据
将2个8位数据high、low合成一个16位数据data_u16:
data_u16 = (high<<8) | low;
参考历程
/*****************************************参考历程***************************************/
/*************************
8位转16位
Len: 数据长度
*Data:数据
*************************/
int16_t Data_u16[20];
void u8tou16(unsigned char Len, unsigned char *Data)
{
unsigned char DataLen_u16, SurplusLen;//16位数据长度和8位数据剩余长度
unsigned char i, num = 0;
SurplusLen = Len % 2;//知道最后空几个
if (SurplusLen != 0) Len += (2 - SurplusLen);
for (i = 0; i < Len; i += 2)
{
//printf("%d\r\n", i);
if (i < (Len - 2))
{
Data_u16[num] |= Data[i + 0] << 8;
Data_u16[num] |= Data[i + 1];
}
else
{
if (SurplusLen == 1)
{
Data_u16[num] |= Data[i + 0] << 8;
Data_u16[num] |= 0;
}
else
{
Data_u16[num] |= Data[i + 0] << 8;
Data_u16[num] |= Data[i + 1];
}
}
num++;
}
}
2.8位数据转32位数据
将4个8位数据RData_U8[4]合成一个32位数据data_u32:
Data_Buff_U32[i] |=RData_U8[0]<<24;
Data_Buff_U32[i] |=RData_U8[1]<<16;
Data_Buff_U32[i] |=RData_U8[2]<<8;
Data_Buff_U32[i] |=RData_U8[3];
参考历程
/*************************
32位转8位
Len: 数据长度
*Data:数据
*************************/
unsigned char Data_u8[40];
void u32tou8(unsigned char Len, unsigned int *Data)
{
unsigned char i=0,num=0;
for (i = 0; i < Len; i++)
{
Data_u8[num + 0] = (unsigned char)((Data[i] >> 24));
Data_u8[num + 1] = (unsigned char)((Data[i] >> 16));
Data_u8[num + 2] = (unsigned char)((Data[i] >> 8));
Data_u8[num + 3] = (unsigned char)(Data[i]);
num += 4;
}
}
3.16位数据转32位数据
将2个16位数据Data_u16[2]合成一个32位数据Data_u32
Data_u32 = (Data_u16[0]<<16)|Data_u16[1];
参考历程
/*************************
16位转32位
Len: 数据长度
*Data:数据
*************************/
void u16tou32(unsigned char Len, int16_t *Data)
{
unsigned char SurplusLen;//8位数据剩余长度
unsigned char i, num = 0;
SurplusLen = Len % 2;//知道最后空几个
if (SurplusLen != 0) Len += (2 - SurplusLen);
for (i = 0; i < Len; i += 2)
{
//printf("%d\r\n", i);
if (i < (Len - 2))
{
Data_u32[num] |= Data[i + 0] << 16;
Data_u32[num] |= Data[i + 1];
}
else
{
if (SurplusLen == 1)
{
Data_u32[num] |= Data[i + 0] << 16;
Data_u32[num] |= 0;
}
else
{
Data_u32[num] |= Data[i + 0] << 16;
Data_u32[num] |= Data[i + 1];
}
}
num++;
}
}
4.16位数据转8位数据
将一个16位数据Data_u16拆分成2个8位数据high、low:
high = (data_u16 >> 8) & 0xff; //高8位
low = data_u16 & 0xff; //低8位
参考历程
/*************************
16位转8位
Len: 数据长度
*Data:数据
*************************/
void u16tou8(unsigned char Len, int16_t *Data)
{
unsigned char i = 0, num = 0;
for (i = 0; i < Len; i++)
{
Data_u8[num + 0] = (unsigned char)((Data[i] >> 8));
Data_u8[num + 1] = (unsigned char)(Data[i]);
num += 2;
}
}
5.32位数据转8位数据
将一个32位数据WData_Buff_32拆分成4个8位数据Data_u8[4]:
//高位在前,低位在后
Data_U8[0] = (uint8_t) ((WData_Buff_32[i] >> 24)&0xff);
Data_U8[1] = (uint8_t) ((WData_Buff_32[i] >> 16)&0xff);
Data_U8[2] = (uint8_t) ((WData_Buff_32[i] >> 8)&0xff);
Data_U8[3] = (uint8_t) (WData_Buff_32[i] &0xff);
参考历程
/*************************
32位转8位
Len: 数据长度
*Data:数据
*************************/
unsigned char Data_u8[40];
void u32tou8(unsigned char Len, unsigned int *Data)
{
unsigned char i=0,num=0;
for (i = 0; i < Len; i++)
{
Data_u8[num + 0] = (unsigned char)((Data[i] >> 24));
Data_u8[num + 1] = (unsigned char)((Data[i] >> 16));
Data_u8[num + 2] = (unsigned char)((Data[i] >> 8));
Data_u8[num + 3] = (unsigned char)(Data[i]);
num += 4;
}
}
6.32位数据转16位数据
将一个32位数据WData_Buff_32拆分成2个16位数据Data_u16[2]:
//高位在前,低位在后
Data_U16[0] = (uint16_t) ((WData_Buff_32[i] >> 16)&0xffff);
Data_U16[1] = (uint16_t) (WData_Buff_32[i] &0xffff);
参考历程
/*************************
32位转16位
Len: 数据长度
*Data:数据
*************************/
void u32tou16(unsigned char Len, unsigned int *Data)
{
unsigned char i = 0, num = 0;
for (i = 0; i < Len; i++)
{
Data_u16[num + 0] = (int16_t)((Data[i] >> 16));
Data_u16[num + 1] = (int16_t)Data[i];
num += 2;
}
}
|