我让这个类来计算byte[]的CRC8校验和:
public static class Crc8
{
static byte[] table = new byte[256];
// x8 + x7 + x6 + x4 + x2 + 1
const byte poly = 0xd5;
public static byte ComputeChecksum(params byte[] bytes)
{
byte crc = 0;
if (bytes != null && bytes.Length > 0)
{
foreach (byte b in bytes)
{
crc = table[crc ^ b];
}
}
return crc;
}
static Crc8()
{
for (int i = 0; i < 256; ++i)
{
int temp = i;
for (int j = 0; j < 8; ++j)
{
if ((temp & 0x80) != 0)
{
temp = (temp << 1) ^ poly;
}
else
{
temp <<= 1;
}
}
table[i] = (byte)temp;
}
}
}大体上我得到了:
static void Main(string[] args)
{
string number = "123456789";
Console.WriteLine(Convert.ToByte(Crc8.ComputeChecksum(StringToByteArray(number))).ToString("x2"));
Console.ReadLine();
}
private static byte[] StringToByteArray(string str)
{
ASCIIEncoding enc = new ASCIIEncoding();
return enc.GetBytes(str);
}这将导致0xBC
但是,根据:http://www.scadacore.com/field-tools/programming-calculators/online-checksum-calculator/,这是不正确的,因为CheckSum8异或的校验和是0x31。
我到底做错了什么?
发布于 2017-06-27 16:19:29
在链接的站点上,仅列出了一些16位和32位CRC,CheckSum8Xor is not a CRC。CRC0xBC循环冗余校验来自名为“-8/-S2”的8位循环冗余校验,请参阅http://reveng.sourceforge.net/crc-catalogue/1-15.htm
发布于 2017-06-28 03:27:58
啊,好吧,我已经过度迭代了这个校验和计算。
好吧,在这种情况下,这很简单:
public static byte Checksum8XOR(byte[] data)
{
byte checksum = 0x00;
for (int i = 0; i < data.Length; i++)
{
checksum ^= data[i];
}
return checksum;
}https://stackoverflow.com/questions/44767611
复制相似问题