# 哪位朋友能帮个忙，把我的这段VC代码改成C#代码！！！

0

（1） 生成CRC初始矩阵
void CRC_Init_CRC32_Table(unsigned long * crc32_table)
{/* Call this function only once to initialize the CRC table. */调用这个函数来初始化一次启表
/* This is the official polynomial used by CRC-32*/这是官方使用的多项式由CRC - 32
/* in PKZip, WinZip and Ethernet.*/
unsigned long ulPolynomial = 0x04c11db7;
int i, j;

/* 256 values representing ASCII character codes.*/
for( i = 0; i <= 0xFF; i++)
{
crc32_table[i]=CRC_Reflect(i, 8) < < 24;
for ( j = 0; j < 8; j++)
crc32_table[i] = (crc32_table[i] < < 1) ^ (crc32_table[i] & (1 < < 31) ? ulPolynomial : 0);
crc32_table[i] = CRC_Reflect(crc32_table[i], 32);
}
}

unsigned long CRC_Reflect(unsigned long ref, unsigned char ch)
{/* Used only by Init_CRC32_Table().*/
unsigned long value=0;
int i;

/* Swap bit 0 for bit 7*/
/* bit 1 for bit 6, etc. */
for( i = 1; i < (ch + 1); i++)
{
if(ref & 1)  value |= 1 < < (ch - i);
ref >>= 1;
}
return value;
}
（2） 计算文件CRC校验码：
unsigned long GetCRC(FILE * fp)
{
unsigned long crc32_table[256], globalCRC;
char lsFileCRC[10];
int i;
unsigned char ch;
int liFileLen;

CRC_Init_CRC32_Table(crc32_table) ;

/*start calculation*/
globalCRC=0xffffffff;
fseek(fp, 0L, SEEK_END);
liFileLen = ftell(fp);
rewind(fp);
for (i=0; i < liFileLen ; i++ )
{
CRC_AddText(crc32_table,&globalCRC,(unsigned char *)&ch , sizeof ch);
}
/*end calculation*/
globalCRC^=0xffffffff;
return globalCRC;
}

void CRC_AddText(unsigned long * crc32_table,unsigned long * pCRC,unsigned char *text,int len)
{
unsigned char * buffer;
buffer=text;
while (len-->0)
*pCRC = (*pCRC >> 8) ^ crc32_table[(*pCRC & 0xFF) ^ *buffer++];
}

wgm | 初学一级 | 园豆：150

您需要登录以后才能回答，未注册用户请先注册