我有一些代码可以在发送方计算CRC,但是我想对其进行修改以使用循环移位寄存器。在输出末尾,我的目标是使寄存器仅包含“ 000”。我发现了几种不同的方法,但是到目前为止没有任何效果。任何帮助,将不胜感激!这是我要修改的CRC代码:
#!/usr/bin/env python3
def crc(msg, div, code='000'):
"""
Arguments:
msg: The input message of which to generate the output code.
div: The divisor in polynomial form.
code: This is an option argument where a previously generated code may
be passed in. If the inputted
code produces an outputted code of all zeros, then the message has
no errors.
Returns:
An error-detecting code generated by the message and the given divisor.
"""
# Append the code to the message. If no code is given, default to '000'
msg = msg + code
# Convert msg and div into list form for easier handling
msg = list(msg)
div = list(div)
# Loop over every message bit (minus the appended code)
for i in range(len(msg)-len(code)):
print(" input:" )
# If that messsage bit is 1, perform modulo 2 multiplication
if msg[i] == '1':
for j in range(len(div)):
# Perform modulo 2 multiplication on each index of the divisor
msg[i+j] = str((int(msg[i+j])+int(div[j]))%2)
print (msg[i+j], end=' ')
# Output the last error-checking code portion of the message generated
return ''.join(msg[-len(code):])
这是一个测试示例:
# TEST 1
####################################################################
print('Test 1 ---------------------------')
# Use a divisor that simulates: x^3 + x + 1
div = '1011'
msg = '11010011101100'
print('Input message:', msg)
print('Divisor:', div)
# Enter the message and divisor to calculate the error-checking code
code = crc(msg, div)
print('Output code:', code)
# Perform a test to check that the code, when run back through, returns an
# output code of '000' proving that the function worked correctly
print('Success:', crc(msg, div, code) == '000')
预期输出:
0 --- 0 -+- 0 -+- input: [‘1’, ‘0’, ‘1’, ‘0’, ‘0’, ‘0’]
0 --- 0 -+- 1 -+- input: [‘0’, ‘1’, ‘0’, ‘0’, ‘0’]
0 --- 1 -+- 0 -+- input: [‘1’, ‘0’, ‘0’, ‘0’]
1 --- 0 -+- 1 -+- input: [‘0’, ‘0’, ‘0’]
0 --- 0 -+- 1 -+- input: [‘0’, ‘0’]
0 --- 1 -+- 0 -+- input: [‘0’]
1 --- 0 -+- 0 -+- input: []
output code: 100