我试图实现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