几个常用的数据处理代码实现

添加几个工作中常用的基本数据处理代码段,以备查阅。

1、crc16校验计算。传入参数:目标数据,数据长度,返回值:crc16校验结果

const uint16_t wCRCTalbeAbs[] ={ 0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401, 0xA001,\
	                             0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400};
uint16_t Crc16(uint8_t* pchMsg, uint16_t wDataLen)
{
	uint8_t  chChar    = 0 ;
	uint16_t i         = 0 ;
	uint16_t wCRC      = 0xFFFF;

	for (i = 0; i < wDataLen; i++)
	{
		chChar = *pchMsg++;
		wCRC = wCRCTalbeAbs[(chChar ^ wCRC) & 15] ^ (wCRC >> 4);
		wCRC = wCRCTalbeAbs[((chChar >> 4) ^ wCRC) & 15] ^ (wCRC >> 4);
	}
	return wCRC;
}

2、大小端转换。

int32_t swapInt32(int32_t value)
{  
     return ((value & 0x000000FF) << 24) | ((value & 0x0000FF00) << 8) | ((value & 0x00FF0000) >> 8) | ((value & 0xFF000000) >> 24);  
}
int16_t swapInt16(int16_t value)
{  
     return (((value) & 0x00ff) << 8 | ((value) & 0xff00) >> 8);
}

3、字符串转16进制数

unsigned int str2shortint(char *str, int len)
{
    unsigned int value = 0 ;
    uint8_t  i         = 0 ;
    char     temp      = 0 ;
    if(str == NULL || len > 8)
    {
        return 0 ;
    }
    for(i = 0;i < len;i++)
    {
        value <<= 4 ;
        if ((str[i] <= '9' && str[i] >= '0'))
        {
            temp = '0';
        }
        else if((str[i] >= 'a' && str[i] <= 'f'))
        {
            temp = 'a' - 0x0A;
        }
        else if((str[i] >= 'A' && str[i] <= 'F'))
        {
            temp = 'A' - 0x0A;
        }
        value |= (str[i] - temp ) & 0x0F ;
    }
    return value;
}

4、字符转BCD码

void ASIICToBCD(uint8_t *src,uint8_t *dest,uint8_t len)
{
	uint8_t temp = 0 ;
	uint8_t loop = 0 ;
	for(loop = 0;loop < (len+1)/2;loop++)
	{
		temp = (*src - '0')&0x0F ;
		src ++ ;
		temp <<= 4 ;
		if((loop + 1) == (len+1)/2 && (len%2 == 1))
		{
			temp |= 0x00 ;
		}
		else
		{
			temp |= (*src - '0')&0x0F ;
			src ++ ;
		}
		dest[loop] = temp ;
	}
}

5、bcd转字符

void BCDToASIIC(uint8_t *src,uint8_t *dest,uint8_t len)
{
	uint8_t temp = 0 ;
	uint8_t loop = 0 ;
	for(loop = 0;loop < len;loop++)
	{
		temp = (*src >> 4 & 0x0F);
		*dest++ = temp + '0' ;
		temp = (*src & 0x0F);
		if(temp != 0x0F)
		{
			*dest++ = temp + '0' ;
			src++ ;
		}
	}
}

6、字符串转16进制

void StrToHex(char *pbDest, char *pbSrc, int nLen)
{
	char h1,h2;
	char s1,s2;
	int i;

	for (i=0; i<nLen; i++)
	{
		h1 = pbSrc[2*i];
		h2 = pbSrc[2*i+1];

		s1 = toupper(h1) - 0x30;
		if (s1 > 9) 
		s1 -= 7;

		s2 = toupper(h2) - 0x30;
		if (s2 > 9) 
		s2 -= 7;

		pbDest[i] = s1*16 + s2;
	}
}

7、16进制转字符串

void HexToStr(char *pbDest, unsigned char *pbSrc, int nLen)
{
	char ddl,ddh;
	int i;
	
//	for( i = 0; i < nLen; i++ )
//	{
//		printf("pbSrc[%d] = %x\n", i, pbSrc[i]);
//	}

	for (i=0; i<nLen; i++)
	{		
		ddh = 48 + pbSrc[i] / 16;
		ddl = 48 + pbSrc[i] % 16;


		/* ´óд×Öĸ */
		if (ddh > 57 && ddh < 96) 
			ddh = ddh + 7;
		if (ddl > 57 && ddl < 96) 
			ddl = ddl + 7;

		pbDest[i*2] = ddh;
		pbDest[i*2+1] = ddl;
	}

	pbDest[nLen*2] = '\0';
}


猜你喜欢

转载自blog.csdn.net/liufei191010/article/details/80842477