各种常用校验方法的源代码
Posted 十日十乞
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了各种常用校验方法的源代码相关的知识,希望对你有一定的参考价值。
常用各种数据校验方法源代码Borland C++ Builder5.0
//-----------------------------------------------------------------------------
//定义数据类型缩写形式
typedef unsigned char uchar; //无符号字符
typedef unsigned short ushort; //无符号短整型
typedef unsigned long ulong; //无符号长整型
typedef unsigned int uint; //无符号整型
typedef DynamicArray<Byte> TByteDynArray; //动态定义字节数组
//CRC8位校验
uchar CRC8Valid(const uchar* puchData, int len)
{
uchar crc8 = 0;
while (len--)
{
crc8 = crc8 ^ (*puchData++);
}
return crc8;
}
//---------------------------------------------------------------------------
//CRC8位校验
uchar CRC8Valid(const TByteDynArray puchData, int len)
{
uchar crc8 = 0;
int index = 0;
while (len--)
{
crc8 = crc8 ^ puchData[index++];
}
return crc8;
}
//---------------------------------------------------------------------------
//和校验
uchar SumValid(const uchar* puchData, int len)
{
uchar crc8 = 0;
while (len--)
{
crc8 = crc8 + (*puchData++);
}
return crc8;
}
//---------------------------------------------------------------------------
//和校验
uchar SumValid(const TByteDynArray puchData, int len)
{
uchar crc8 = 0;
int index = 0;
while (len--)
{
crc8 = crc8 + puchData[index++];
}
return crc8;
}
//---------------------------------------------------------------------------
//从第index位开始进行校验
uchar SumValid(const uchar* puchData, int index,int len)
{
uchar crc8 = 0;
puchData+=index;
while (len--)
{
crc8 = crc8 + (*puchData++);
}
return crc8;
}
//---------------------------------------------------------------------------
//从第index位开始进行校验
uchar SumValid(const TByteDynArray puchData, int index,int len)
{
uchar crc8 = 0;
int indexs = 0;
indexs+=index;
while (len--)
{
crc8 = crc8 + puchData[indexs++];
}
return crc8;
}
//---------------------------------------------------------------------------
//CRC16校验
ushort CRC16Valid(const TByteDynArray buffer, int len)
{
ushort crc = 0, tmp;
int index = 0;
while(len--)
{
tmp = buffer[index++];
crc = crc ^ (tmp << 8);
for (int i = 0; i < 8; i++)
{
if (crc & 0x8000)
crc = (crc << 1) ^ 0x1021;
else
crc <<= 1;
}
}
crc = (crc >> 8) | (crc << 8);
return(crc);
}
//---------------------------------------------------------------------------
//CRC16校验
ushort CRC16Valid(const uchar* buffer, int len)
{
ushort crc = 0, tmp;
while(len--)
{
tmp = *buffer++;
crc = crc ^ (tmp << 8);
for (int i = 0; i < 8; i++)
{
if (crc & 0x8000)
crc = (crc << 1) ^ 0x1021;
else
crc <<= 1;
}
}
crc = (crc >> 8) | (crc << 8);
return(crc);
}
//------------------------------------------------------------------------------
//异或校验
void XORValid(const TByteDynArray buffer, int len)
{
char checksum = 0, cr = 13, ln = 10;
char ch1,c1; //校验位的高四位和第四位
for (int i = 0; i<len; i++)
{
checksum = checksum ^ buffer[i+1]; //进行异或交验取值
}
ch1 = (checksum >> 4) & 0x0F; //取高位数;
c1 = checksum & 0x0F; //取低位数;
if (ch1 < 10) //低于10的数
ch1 = ch1 + ‘0‘;
else
ch1 = (ch1 - 10 ) + ‘A‘; //不低于10的16进制数,如:A、B、C、D、E、F
if (c1 < 10)
c1 = c1 + ‘0‘;
else
c1 = (c1 - 10 )+ ‘A‘;
XOR_High=ch1;
XOR_Low =c1;
}
//-----------------------------------------------------------------------------
//异或校验
void XORValid(const uchar* buffer, int len)
{
char checksum = 0, cr = 13, ln = 10;
char ch1,c1; //校验位的高四位和第四位
for (int i = 0; i<len; i++)
{
checksum = checksum ^ buffer[i+1]; //进行异或交验取值
}
ch1 = (checksum >> 4) & 0x0F; //取高位数;
c1 = checksum & 0x0F; //取低位数;
if (ch1 < 10) //低于10的数
ch1 = ch1 + ‘0‘;
else
ch1 = (ch1 - 10 ) + ‘A‘; //不低于10的16进制数,如:A、B、C、D、E、F
if (c1 < 10)
c1 = c1 + ‘0‘;
else
c1 = (c1 - 10 )+ ‘A‘;
XOR_High=ch1;
XOR_Low =c1;
}
以上是关于各种常用校验方法的源代码的主要内容,如果未能解决你的问题,请参考以下文章