我需要将这些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;
}
答案 0 :(得分:3)
我觉得很好,除非你真的不需要ref
的{{1}}参数,因为你还是要返回修改后的值。
答案 1 :(得分:0)
您是否尝试过针对各种不同的值运行测试?
也可以让他们static
函数(如果那不是你的计划),因为他们似乎不需要访问任何对象状态。