我遇到了一个问题并且正在努力工作我无法找到比较出错的方法。我正在尝试做多项式长除模2。
while len(binCrcGen) <= len(binPayload) and binPayload:
if binPayload[0] == binCrcGen[0]:
del binPayload[0]
for j in range(len(binCrcGen)-1):
if binPayload[j] == binCrcGen[j+1]:
binPayload.insert(binPayload[j],0)
del binPayload[j+1]
print(binPayload, "binPayload")
else:
binPayload.insert(binPayload[j],1)
del binPayload[j+1]
print(binPayload, "binPayload")
然而我的输出给了我:
Input the binary data payload: 10010
Input the binary representation of the CRC generator: 11
[1, 0, 1, 0] binPayload
[1, 1, 0] binPayload
[1, 0] binPayload
[1] binPayload -- wrong here 1 xor 1 should bring 0 to list.
[1] Remainder
[1, 1] binCrCGen
The data transmitted with the payload 1001 is protected by the CRC 11 is 10011
当手动计算时,我得到剩余的00,当空白列表被删除时,它会变成空白列表。
答案 0 :(得分:2)
您的错误出现在以下几行:
binPayload.insert(binPayload[j],0)
binPayload.insert(binPayload[j],1)
您没有在索引j
处插入。您将在索引0或1处插入,具体取决于binPayload[j]
。要解决此问题,请将索引更改为j
而不是binPayload[j]
。当你在它的时候,你可以删除你正在使用的奇怪的插入/删除东西,只需使用binPayload[j] = whatever
。此外,您可以使用exclusive-or operator ^
删除if / else,并且可以使用就地版本^=
来保存一些重复:
binCrcGen = [1,1]
binPayload = [1,0,0,1,0]
while len(binCrcGen) <= len(binPayload) and binPayload:
if binPayload[0] == binCrcGen[0]:
del binPayload[0]
for j in range(len(binCrcGen)-1):
binPayload[j] ^= binCrcGen[j+1]
print(binPayload, "binPayload")
while binPayload and binPayload[0] == 0:
del binPayload[0]
print(binPayload, "binPayload")