我需要将这些C函数转换为C#。只是想仔细检查一下我是否做得对。谢谢!CRC功能。将C转换为C#
C代码:
unsigned short Crc;
unsigned short update_crc(unsigned short crc, char c) {
char i;
crc ^= (unsigned short)c<<8;
for (i=0; i<8; i++) {
if (crc & 0x8000) crc = (crc<<1)^0x1021;
else crc <<=1;
}
return crc;
}
void exampleCRC(void){
#define INITIAL_CRC 0xffff
unsigned short Crc = INITIAL_CRC;
record_t record;
for (byteCount=0; byteCount<sizeof(record_t); byteCount++) {
Crc = update_crc(Crc, record[byteCount]);
}
}
C#代码:
ushort UpdateCrc(ref ushort crc, byte b)
{
crc ^= (ushort)(b << 8);
for (int i = 0; i < 8; i++)
{
if ((crc & 0x8000) > 0)
crc = (ushort)((crc << 1)^0x1021);
else
crc <<= 1;
}
return crc;
}
ushort CalcCrc(byte[] data)
{
ushort crc = 0xFFFF;
for (int i = 0; i < data.Length; i++)
crc = UpdateCrc(ref crc, data[i]);
return crc;
}
两个字:单元测试! – vcsjones 2010-11-15 03:46:45
是的,好主意。谢谢! – Matt 2010-11-15 03:52:32