2021年3月9日星期二

CRC implementation in python

Here is my code for the implementation of a CRC in python:

import math    divisors  = [0b1100000001111, 0b11000000000000101, 0b10001000000100001, 0b1011, 0b10011,0b00000111, 0b11001]    def get_Length(arg):      return math.floor(math.log2(arg)) +1    def CRC(message, type):      print("Message ",bin(message)[2:], hex(message))      # int message_length = get_Length(message);      divisor_length = get_Length(divisors[type])      divisor = divisors[type]      print("Divisor: ",bin(divisor)[2:], hex(divisor))      message = message << (divisor_length-1)        old_message = message      while( (message >> (divisor_length-1)) !=0 ):          ml = get_Length(message)            divisor_copy = divisor << (ml-divisor_length)            message = message ^ divisor_copy        print(bin(message)[2:], hex(message))      print(bin(old_message| message)[2:], hex(old_message|message), end="\n\n")    def main():     CRC(0b1101011011, 4)     CRC(0x34ec, 1)  main()  

The first message is from this Wikipedia example and gives the correct result. However the second one (x34ec), which demonstrates a CRC-16 is not giving the correct result (correct result). I'm attaching the output snapshot as well:

enter image description here.

It would be appreciative if somebody can shed some light on it.

Thanks in advance.

https://stackoverflow.com/questions/66554786/crc-implementation-in-python March 10, 2021 at 05:07AM

没有评论:

发表评论