0
所以我有這樣的C代碼,我需要移植到C#:移植CRC16代碼在C C#.NET
C代碼:
uint16 crc16_calc(volatile uint8* bytes, uint32 length)
{
uint32 i;
uint32 j;
uint16 crc = 0xFFFF;
uint16 word;
for (i=0; i < length/2 ; i++)
{
word = ((uint16*)bytes)[i];
// upper byte
j = (uint8)((word^crc) >> 8);
crc = (crc << 8)^crc16_table[j];
// lower byte
j = (uint8)((word^(crc >> 8)) & 0x00FF);
crc = (crc << 8)^crc16_table[j];
}
return crc;
}
閥塊的C#代碼:
public ushort CalculateChecksum(byte[] bytes)
{
uint j = 0;
ushort crc = 0xFFFF;
ushort word;
for (uint i = 0; i < bytes.Length/2; i++)
{
word = bytes[i];
// Upper byte
j = (byte)((word^crc) >> 8);
crc = (ushort)((crc << 8)^crc16_table[j]);
// Lower byte
j = (byte)((word^(crc >> 8)) & 0x00FF);
crc = (ushort)((crc << 8)^crc16_table[j]);
}
return crc;
}
此C算法使用查找表crc16_table計算提供的字節的CRC16 [j]
但是,Ported C#代碼會執行n不會產生與C代碼相同的結果,我是否做錯了什麼?
或使用BitConverter.ToInt16 – MerickOWA