重赏高手!
lxm75 2006-08-03 12:38:58 我从网络上搜集了大量关于CRC32的计算,可没有一个能正确计算出我想要的结果,我不知道到底我错在哪里,故将代码传上来,请高手指点!
鄙人无以回报,只能许诺将自己完全自主开发并已销售在百宝箱上线的游戏完整原代码(KJava的代码,与C语言语法相似,得此代码您将可以轻松地编写出同种类型游戏)真诚地奉献给您!
我整理的代码如下:
// crc3.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
//---------------------------------------------------------------------------
//算法四
static unsigned long crc32_table4[256]; // Lookup table array
static unsigned long Reflect(unsigned long ref, char ch)
{
// Used only by Init_CRC32_Table().
unsigned long value = 0;
// Swap bit 0 for bit 7
// bit 1 for bit 6, etc.
int i;
for(i = 1; i < (ch + 1); i++)
{
if(ref & 1)
value |= 1 << (ch - i);
ref >>= 1;
}
return value;
}
void Init_CRC32_Table()
{
// Call this function only once to initialize the CRC table.
// This is the official polynomial used by CRC-32
// in PKZip, WinZip and Ethernet.
unsigned long ulPolynomial = 0x04c11db7;
// 256 values representing ASCII character codes.
int i, j;
for(i = 0; i <= 0xFF; i++)
{
crc32_table4[i] = Reflect(i, 8) << 24;
for (j = 0; j < 8; j++)
crc32_table4[i] = (crc32_table4[i] << 1) ^ (crc32_table4[i] & (1UL << 31) ? ulPolynomial : 0);
crc32_table4[i] = Reflect(crc32_table4[i], 32);
}
}
unsigned long Get_CRC32(unsigned char *pdata, int len)
{
// Pass a text string to this function and it will return the CRC.
// Once the lookup table has been filled in by the two functions above,
// this function creates all CRCs using only the lookup table.
// Be sure to use unsigned variables,
// because negative values introduce high bits
// where zero bits are required.
// Start out with all bits set high.
unsigned long ulCRC = 0xffffffff;
// Perform the algorithm on each character
// in the string, using the lookup table values.
while(len--)
ulCRC = (ulCRC >> 8) ^ crc32_table4[(ulCRC & 0xFF) ^ (unsigned char)(*pdata++)];
// Exclusive OR the result with the beginning value.
return ulCRC ^ 0xffffffff;
}
//---------------------------------------------------------------------------