CRC算法实现

时间:2016-07-04 21:44:14

标签: c algorithm crc crc16

我在C中实现CRC16算法:

$ ls existing-file non-existent-file > tmp 2>&1 > tmp1 $ cat tmp1 existing-file $ cat tmp ls: non-existent-file: No such file or directory

现在的问题是......如果我在第一次比较后更改了初始值,它将始终等于数据流。

例如:如果我得到初始值

init = 0x0000 as long as the data stream goes on if the first bit of data is not equal to the first bit of initial value init = leftshift init once and xor it with the polynomial otherwise leftshift init go to the next data bit init = CRC Checksum

和数据流

1011 0000 1011 0101

第一次迭代后

它们将始终相等,因为开头的0011 0110 1000 0101无关紧要,可以忽略。

在下一次迭代之后,它们将成为:

0's

分别是

和数据流

011 0000 1011 0101

但同样可以忽略011 0110 1000 0101,我们得到平等。

我真的很困惑。

这是我的C代码:

0's

1 个答案:

答案 0 :(得分:2)

几个问题:

  1. 您在最低位操作,但应该处理最重要的位。这可能会导致您对保持不变的位置产生混淆,因为您正在查看错误的值。

  2. crc & 1 && data & 1检查这些位是否等于1而不是检查它们是否相等。

  3. 您似乎对data是否为数组(如声明),整数(在data & 1中使用)或指针(在{{1}中使用)感到困惑})。

  4. 如果将data++更改为指针并在每一步将其递增1,则意味着您只处理每个输入字节中的一位。你需要一个内部循环来处理所有8位。