编辑代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
/*
// C prototype : void StrToHex(unsigned char *pbDest, unsigned char *pbSrc, int nLen)
// parameter(s): [OUT] pbDest - 输出缓冲区
// [IN] pbSrc - 字符串
// [IN] nLen - 16进制数的字节数(字符串的长度/2)
// return value:
// remarks : 将字符串转化为16进制数
*/
void StrToHex(unsigned char *pbDest, unsigned char *pbSrc, int nLen)
{
	char h1,h2;
	unsigned 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;
		}
}

unsigned char GetCrcValue(unsigned char* buf, unsigned char crclength)
{
    unsigned char i,crcreturn = 0;

    for(i= 1; i< crclength; i++ )
    {
        crcreturn ^= buf[i];
    }
    return crcreturn; 
}


int main()
{
	 char tmp[64] = "E50003021011005E";
	unsigned char out[32] = {0};
	memset(out, 0 ,8);
	
	
	StrToHex(out, tmp, 8);
	
	
	int i;
	for(i=0; i<8; i++)
	{
		printf("out[%d] : %x \n", i, out[i]);
	}
	
	int crc = 0;
    crc = GetCrcValue(out,6);
    printf("CRC value: %d\n",crc);
    printf("CRC value2 %d",229^0^3^2^16^17);
	return 0;
}