makispaiktis

CRC Code

Jun 30th, 2021 (edited)
1,227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.18 KB | None | 0 0
  1. from random import randrange
  2.  
  3. # Function 1 - Calculate XOR
  4. def XOR(number1, number2):
  5.     if len(number1) != len(number2):
  6.         print("Error while calculating function '" + XOR.__name__ + "'")
  7.         return -1000
  8.     # print("XOR between", number1, number2)
  9.     N = len(number1)
  10.     result = ""
  11.     for i in range(N):
  12.         if number1[i] == number2[i]:
  13.             result += "0"
  14.         else:
  15.             result += "1"
  16.     return result
  17.  
  18.  
  19. # Function 2 - Simulate
  20. def simulate():
  21.     # 1. Create a bitstream named "D"
  22.     limit1 = 10
  23.     limit2 = 20
  24.     length = randrange(limit1, limit2+1)
  25.     D = [str(randrange(2)) for i in range(length)]
  26.     D = "".join(D)
  27.     d = len(D)
  28.  
  29.     # 2. Create a random number
  30.     limit1 = 0.3 * limit1
  31.     limit2 = 0.3 * limit2
  32.     length = randrange(int(limit1), int(limit2)+1)
  33.     R_initial = [str(0) for i in range(length)]
  34.     R_initial = "".join(R_initial)
  35.     r = len(R_initial)
  36.     DR_initial = D + R_initial
  37.     # print(D, R_initial, DR_initial)
  38.  
  39.     # 3. Create a generator function G. It must begin with 1
  40.     g = r + 1
  41.     G = [str(randrange(2)) for i in range(g)]
  42.     G[0] = "1"
  43.     G = "".join(G)
  44.     # print()
  45.     # print(G, "    ", DR_initial)
  46.  
  47.     # 4. Ready to divide with XOR logic
  48.     # First, I will produce G' = G * 0 (same digits number)
  49.     GG = ["0" for i in range(g)]
  50.     GG = "".join(GG)
  51.     times = len(DR_initial) + 1 - g
  52.     result = ""
  53.     # Initialization
  54.     segment = DR_initial[0:g]
  55.     segment = "".join(segment)
  56.     if segment[0] == "1":
  57.         result = XOR(segment, G)
  58.         # print("Seg = " + segment + ", G = " + G + ", result = " + result)
  59.     else:
  60.         result = XOR(segment, GG)
  61.         # print("Seg = " + segment + ", GG = " + GG + ", result = " + result)
  62.  
  63.     for i in range(times-1):
  64.         # Now, result has length = g, but the first digit IS ALWAYS 0
  65.         # So, I will remove the first zero and then I will add in the letter's word the next digit of D * 2^r
  66.         segment = result[1:]
  67.         segment += DR_initial[i+g]
  68.         segment = "".join(segment)
  69.         if segment[0] == "1":
  70.             result = XOR(segment, G)
  71.             # print("Seg = " + segment + ", G = " + G + ", result = " + result)
  72.         else:
  73.             result = XOR(segment, GG)
  74.             # print("Seg = " + segment + ", GG = " + GG + ", result = " + result)
  75.  
  76.     # After all this procedure, we have ended with a g-length word "result"
  77.     # The 1st bit of the word is for sure "0", because of our XOR logic
  78.     # r = g-1 = the number of EDC bits ----> so, I have to remove the 1st bit = 0 from "result"
  79.     result = result[1:]
  80.     print("*******************************************************************************")
  81.     print("D = " + D + "    G = " + G + "       (d = " + str(d) + ", g = " + str(g) + ", r = " + str(r) + ")")
  82.     print("D * 2^r = " + DR_initial)
  83.     print()
  84.     print(G + "    |     " + DR_initial)
  85.     print("--------------------------------")
  86.     R = result
  87.     DR_final = D + R
  88.     print("R = " + R)
  89.     print()
  90.     print("<D,R> = " + DR_final)
  91.     print("*******************************************************************************")
  92.  
  93.  
  94. # MAIN FUNCTION
  95. simulate()
Add Comment
Please, Sign In to add comment