repl.it
Python

No description

fork
loading
Files
  • main.py
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Helper Function
def binary(string):
	answer = []
	for i in string:
		answer.append(bool(int(i)))
	return answer

def string(binary):
	answer = ""
	for i in binary:
		answer += str(int(i))
	return answer

# Configuration Constants
def Config(poly, init=None, ref_in=False, ref_out=False, xor_out=None):
  if init == None: init = [False] * len(poly)
  if xor_out == None: xor_out = [False] * len(poly)
  return {
    "poly": poly,
    "init": init,
    "ref_in": ref_in,
    "ref_out": ref_out,
    "xor_out": xor_out
  }

CRC_8 = Config(poly=binary("00000111"))
CRC_8_CDMA2000 = Config(poly=binary("10011011"), init=binary("11111111"))
CRC_8_DARC = 
#CRC_8_DVB_S2 = 
#CRC_8_EBU = 
#CRC_8_I_CODE = 
#CRC_8_ITU = 
#CRC_8_MAXIM = 
#CRC_8_ROHC = 
#CRC_8_WCDMA = 

CRC_16_XMODEM = Config(poly=binary("0001000000100001"))

# CRC Calculator
def crc(message_, config, debug=False):
  divisor = [True] + config["poly"]
  length = len(divisor)
  message = binary(message_) + ([False] * (length-1))
  if debug: print(string(divisor))
  while len(message) >= length:
    if message[0]:
      for i in range(length):
        if divisor[i]:
          message[i] = not message[i]
    if debug: print(string(message))
    message = message[1:]
  return string(message)