我試圖實現16-CRC [DNP]使用C#中,generator polynomial
給出如何使用C#實現CRC-16-DNP?
我發現16-CRC的標準溶液:[Source]
public class Crc16
{
const ushort polynomial = 0xA001;
ushort[] table = new ushort[256];
public ushort ComputeChecksum (byte[] bytes)
{
ushort crc = 0;
for (int i = 0; i < bytes.Length; ++i)
{
byte index = (byte) (crc^bytes[i]);
crc = (ushort) ((crc >> 8)^table[index]);
}
return crc;
}
public byte[] ComputeChecksumBytes (byte[] bytes)
{
ushort crc = ComputeChecksum (bytes);
return BitConverter.GetBytes (crc);
}
public Crc16()
{
ushort value;
ushort temp;
for (ushort i = 0; i < table.Length; ++i)
{
value = 0;
temp = i;
for (byte j = 0; j < 8; ++j)
{
if (((value^temp) & 0x0001) != 0)
{
value = (ushort) ((value >> 1)^polynomial);
}
else
{
value >>= 1;
}
temp >>= 1;
}
table[i] = value;
}
}
}
現在,如果我轉換我的多項式我得到1 0011 1101 0110 0111
=>(3D65)h
&我的問題是我需要改變爲給定多項式的上述解決方案。
Edit
:我還需要考慮兩件事情,
1)初始值爲0 &
2)最後的CRC必須加以補充。
對不起,我以前沒有清楚,請參閱上面的編輯部分,也請你解釋你是如何得到'0XA6BC'? – SanVEE 2014-09-19 16:07:36