Created
January 27, 2013 22:29
-
-
Save evansneath/4650991 to your computer and use it in GitHub Desktop.
Performs a cyclic redundancy check implemented in Python 3.3 with examples. More about CRC can be found here: http://en.wikipedia.org/wiki/Cyclic_redundancy_check
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
def crc(msg, div, code='000'): | |
"""Cyclic Redundancy Check | |
Generates an error detecting code based on an inputted message | |
and divisor in the form of a polynomial representation. | |
Arguments: | |
msg: The input message of which to generate the output code. | |
div: The divisor in polynomial form. For example, if the polynomial | |
of x^3 + x + 1 is given, this should be represented as '1011' in | |
the div argument. | |
code: This is an option argument where a previously generated code may | |
be passed in. This can be used to check validity. 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)): | |
# 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) | |
# 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') | |
# TEST 2 #################################################################### | |
print('Test 2 ---------------------------') | |
# Use a divisor that simulates: x^2 + 1 | |
div = '0101' | |
msg = '00101111011101' | |
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') | |
Used this for a homework of mine. I thought perhaps the string lists could be replaced with integer lists:
def crc(msg, div, code='000'):
msg_bits = [int(bit) for bit in msg]
div_bits = [int(bit) for bit in div]
for i in range(len(msg_bits) - len(div_bits)):
if msg_bits[i] == 1:
for j in range(len(div_bits)):
msg_bits[i + j] = msg_bits[i + j] ^ div_bits[j]
return ''.join(map(str, msg_bits[-len(code):]))
I don't think it really matters but it felt more convenient to me.
Anyway, thx for the solution!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you.