,如果你需要CRC CCITT 16克米特你需要下面的代码(从我的网站):
var crc16Kermit = new Crc16(Crc16Mode.CcittKermit);
var checksum = crc16Kermit.ComputeChecksumBytes(0x01, 0x23, 0x45);
// checksum = 0x2e, 0x46
这里的源以上代码
using System;
public enum Crc16Mode : ushort { Standard = 0xA001, CcittKermit = 0x8408 }
public class Crc16 {
static ushort[] table = new ushort[256];
public ushort ComputeChecksum(params 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(params byte[] bytes) {
ushort crc = ComputeChecksum(bytes);
return BitConverter.GetBytes(crc);
}
public Crc16(Crc16Mode mode) {
ushort polynomial = (ushort)mode;
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;
}
}
}
上述代码的链接:http://sanity-free.org/147/standard_crc16_and_crc16_kermit_implementation_in_csharp.html
我的第一个答案的道歉,我点击了错误的网址,最终给你简单的CRC代码。 – Polynomial
任何特别的原因,这是得到downvoted? – Polynomial