110,539
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#define SIZEOF_CRC32 4
#define KEY_POLYNOMIAL 0x04C11DB7
unsigned int CalculateCRC32(const unsigned char* pDataStream, unsigned int uiLength, unsigned int lCRCKey)
{
if(pDataStream == NULL)
{
return -1;
};
if ( !uiLength | !lCRCKey)
return -1;
//The CRC processing array (BYTE) comprises of the CRC followed by a data byte
//The data byte is used to store the last byte to be processed by shifting through
//the CRC buffer (like a shift register).
int i, j, iCRC = 0;
char cByte = 0;
int iBytesAdded = 0;
int nCarry = 0;
//bitwise 32 bit modulo long division based on bytes of binary data
//retain remainder only, not the result
for (i = 0; i < ( SIZEOF_CRC32 + uiLength); i++) //correct - flush
{
//Shift in more bytes of the dividend
if ( iBytesAdded < uiLength)
{
cByte = pDataStream[iBytesAdded++];
}
else
{
cByte = 0;
}
//shift the CRC32 for 8 bits, one byte
for (j = 0; j < 8; j++)
{
if( iCRC < 0)
{
nCarry = 1 ;
}
else
{
nCarry = 0;
}
iCRC <<= 1;
//shift dividend polynomial left by one bit
if (cByte < 0) iCRC |= 1;
//add in carry to lsbit
cByte <<= 1;
if (nCarry) iCRC ^= lCRCKey;
}
}
return (unsigned int) iCRC;
}
int main(int argc, char *argv[])
{
char i, count, value[30];
printf("请输入字节回车,00 0A 12 00 01 90,计算结果应为6FF93724:\n");
count=0;
do
{
scanf("%x",&value[count++]);
}
while(getchar()!='\n');
printf("message len is 0x%x\n", count);
unsigned int crc = CalculateCRC32(value, count, KEY_POLYNOMIAL);
printf("crc32 is 0x%x\n", crc);
system("PAUSE");
return 0;
}
public static int SIZEOF_CRC32 = 4;
public static int KEY_POLYNOMIAL = 0x04C11DB7;
public static int CalculateCRC32(char[] pDataStream, int uiLength, int lCRCKey)
{
if (pDataStream == null)
{
return -1;
};
if (uiLength == 0 || lCRCKey == 0)
return -1;
int i, j, iCRC = 0;
char cByte = '0';
int iBytesAdded = 0;
int nCarry = 0;
//bitwise 32 bit modulo long division based on bytes of binary data
//retain remainder only, not the result
for (i = 0; i < (SIZEOF_CRC32 + uiLength); i++) //correct - flush
{
//Shift in more bytes of the dividend
if (iBytesAdded < uiLength)
{
cByte = pDataStream[iBytesAdded++];
}
else
{
cByte = '0';
}
//shift the CRC32 for 8 bits, one byte
for (j = 0; j < 8; j++)
{
if (iCRC < 0)
{
nCarry = 1;
}
else
{
nCarry = 0;
}
iCRC <<= 1;
//shift dividend polynomial left by one bit
if (cByte < 0) iCRC |= 1;
//add in carry to lsbit
cByte <<= 1;
if (nCarry == 0)
iCRC ^= lCRCKey;
}
}
return (int)iCRC;
}
static void Main(string[] args)
{
int count = 0;
char[] value=new char[30] ;
Console.WriteLine("请输入字节回车,00 0A 12 00 01 90,计算结果应为6FF93724:\n");
string s = Console.ReadLine();
foreach (char c in s.ToCharArray()) {
value[count++] = c;
}
Console.WriteLine("message len is {0}", value.Length);
int crc = CalculateCRC32(value, count, KEY_POLYNOMIAL);
Console.WriteLine("crc32 is {0}", crc.ToString("x8"));
Console.ReadLine();
}
这是按照你的写出来的但是结果和你说的不一样
public static byte[] crc(byte[] src)
{
int len = src.Length;
UInt16 CRC = 0xffff;
UInt16 next;
bool carry;
UInt16 n;
int i = 0;
while (len-- > 0)
{
next = (UInt16)src[i];
CRC ^= next;
for (n = 0; n < 8; n++)
{
carry = (CRC & 1) != 0;
CRC >>= 1;
if (carry)
{
CRC ^= 0xA001;
}
}
i++;
}
var a = BitConverter.GetBytes(CRC);
return a;
}
如果不是 你也不要着急 直接搜c# crc32校验就可以搜索到相关信息,.