我正在嘗試第一次計算 CRC。我已經閱讀了幾頁,解釋了什么是 crc 以及如何計算。主要是這個:
有人能幫我理解這里有什么問題嗎?提前致謝。
uj5u.com熱心網友回復:
您在計算中遺漏了一點偏移。改變:
ACC = buf[byte];
到:
ACC = (unsigned)buf[byte] << 8;
與 Sunshine 的理解和實作 CRC(回圈冗余校驗)計算中的此 C# 示例代碼進行比較:
public static ushort Compute_CRC16_Simple(byte[] bytes)
{
const ushort generator = 0x1021; /* divisor is 16bit */
ushort crc = 0; /* CRC value is 16bit */
foreach (byte b in bytes)
{
crc ^= (ushort(b << 8); /* move byte into MSB of 16bit CRC */
for (int i = 0; i < 8; i )
{
if ((crc & 0x8000) != 0) /* test for MSB = bit 15 */
{
crc = (ushort((crc << 1) ^ generator);
}
else
{
crc <<= 1;
}
}
}
return crc;
}
它對您的函式使用不同的初始值,但請注意將位元組異或到 16 位 CRC 的 MSB 的行:
crc ^= (ushort(b << 8); /* move byte into MSB of 16bit CRC */
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/378670.html
上一篇:C-如何用某些數字填充陣列
