Created
April 1, 2019 22:44
-
-
Save CptTZ/79db83223075d5e013e6d9b40272f233 to your computer and use it in GitHub Desktop.
CRC Calculation in Python 3
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
def crc_remainder(input_bitstring, polynomial_bitstring, initial_filler): | |
''' | |
Calculates the CRC remainder of a string of bits using a chosen polynomial. | |
initial_filler should be '1' or '0'. | |
''' | |
polynomial_bitstring = polynomial_bitstring.lstrip('0') | |
len_input = len(input_bitstring) | |
initial_padding = initial_filler * (len(polynomial_bitstring) - 1) | |
input_padded_array = list(input_bitstring + initial_padding) | |
while '1' in input_padded_array[:len_input]: | |
cur_shift = input_padded_array.index('1') | |
for i in range(len(polynomial_bitstring)): | |
input_padded_array[cur_shift + i] = str(int(polynomial_bitstring[i] != input_padded_array[cur_shift + i])) | |
return ''.join(input_padded_array)[len_input:] | |
def crc_check(input_bitstring, polynomial_bitstring, check_value): | |
''' | |
Calculates the CRC check of a string of bits using a chosen polynomial. | |
''' | |
polynomial_bitstring = polynomial_bitstring.lstrip('0') | |
len_input = len(input_bitstring) | |
initial_padding = check_value | |
input_padded_array = list(input_bitstring + initial_padding) | |
while '1' in input_padded_array[:len_input]: | |
cur_shift = input_padded_array.index('1') | |
for i in range(len(polynomial_bitstring)): | |
input_padded_array[cur_shift + i] = str(int(polynomial_bitstring[i] != input_padded_array[cur_shift + i])) | |
return ('1' not in ''.join(input_padded_array)[len_input:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment