首页新闻找找看学习计划

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

0
悬赏园豆:50 [已关闭问题]

校验码生成算法如下:(以计算文件校验码为例)
(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++ )
{
fread(&ch,1,1,fp);
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的主页 wgm | 初学一级 | 园豆:150
提问于:2010-01-01 22:51
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册