Skip to content

Instantly share code, notes, and snippets.

@Leovaldez42
Created December 11, 2020 17:41
Show Gist options
  • Save Leovaldez42/263287a647433ba268b005dff07e445e to your computer and use it in GitHub Desktop.
Save Leovaldez42/263287a647433ba268b005dff07e445e to your computer and use it in GitHub Desktop.
def xor(a, b):
result = []
for i in range(1, len(b)):
if a[i] == b[i]: result.append('0')
else: result.append('1')
return ''.join(result)
def mod2div(divident, divisor):
pick = len(divisor)
tmp = divident[0 : pick]
while pick < len(divident):
if tmp[0] == '1':
tmp = xor(divisor, tmp) + divident[pick]
else:
tmp = xor('0'*pick, tmp) + divident[pick]
pick += 1
if tmp[0] == '1':
tmp = xor(divisor, tmp)
else:
tmp = xor('0'*pick, tmp)
checkword = tmp
return checkword
def encodeData(data, key):
l_key = len(key)
appended_data = data + '0'*(l_key-1)
remainder = mod2div(appended_data, key)
codeword = data + remainder
print("Remainder : ", remainder)
print("Encoded Data (Data + Remainder) : ", codeword)
return codeword
# Driver code
data = input("Input Message sequence : ")
key = input("Input Generator Gpolynomial : ")
encoded_codeword = encodeData(data, key)
received = input("Received Message sequence : ")
if received == encoded_codeword:
print("Received codeword is correct.")
else:
print("Received codeword is wrong.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment