Rozwiązanie zadania "CRC" #24
28
main.py
28
main.py
@ -1,6 +1,5 @@
|
||||
if __name__ == "__main__":
|
||||
|
||||
message = format(int.from_bytes(input().encode("ASCII"), "big"), "b")
|
||||
def encode(M):
|
||||
message = format(int.from_bytes(M.encode(), "big"), "b")
|
||||
generator = "10001000000100001"
|
||||
gen_deg = len(generator)
|
||||
|
||||
@ -23,9 +22,28 @@ if __name__ == "__main__":
|
||||
message = message[1:]
|
||||
else:
|
||||
chunk = message[:gen_deg]
|
||||
|
||||
if len(chunk) < gen_deg:
|
||||
print(hex(int(chunk, 2)))
|
||||
break
|
||||
chunk = "00" + chunk
|
||||
return chr(int(chunk[:8], 2)) + chr(int(chunk[8:], 2))
|
||||
|
||||
remainder = format(int(chunk, 2) ^ int(generator, 2), "b")
|
||||
message = remainder + message[gen_deg:]
|
||||
|
||||
|
||||
def decode(N):
|
||||
message = format(int.from_bytes(N.encode(), "big"), "b")
|
||||
|
||||
while len(message) % 8 != 0:
|
||||
message = "0" + message
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
M = input()
|
||||
N = encode(M)
|
||||
# print(N)
|
||||
decode(M+N)
|
||||
# 0011110110111000
|
||||
# 11000010111100101111001001111011100001010111000
|
||||
# 0011110110111000
|
Loading…
Reference in New Issue
Block a user