diff --git a/Zadanie3 b/Zadanie3 index 8780d01..807c69e 100644 --- a/Zadanie3 +++ b/Zadanie3 @@ -1,3 +1,9 @@ +def toBinary(string): + return "".join([format(ord(char),'#010b')[2:] for char in string]) + +def toString(binaryString): + return "".join([chr(int(binaryString[i:i+8],2)) for i in range(0,len(binaryString),8)]) + #XOR def xor(a, b): @@ -44,31 +50,37 @@ def divide(message, poly16): check = temp return check - -def encode(message, poly16): +def decode(message_final,poly16): l_poly16 = len(poly16) - - # Dodajemy n-1 "0" do wiadomosci - appended_message = message + '0'*(l_poly16-1) - remainder = divide(appended_message, poly16) - - message_final = message + remainder - print("calosc : ", - message_final) - - - -def decode(message2, poly16): - - l_poly16 = len(poly16) - appended_message2 = message2 + appended_message2 = message_final remainder = divide(appended_message2, poly16) + #sprawdzenie kazdego bitu for i in range(0, len(remainder)): if '1' not in remainder: print("true ") - print("false ") + elif '1' in remainder: + print("false ") + + +def encode(message, poly16): + #konwersja z ascii do binarnego + aa=toBinary(message) + + l_poly16 = len(poly16) + + # Dodaj n-1 "0" do wiadomosci + appended_message = aa + '0'*(l_poly16-1) + remainder = divide(appended_message, poly16) + + message_final = aa + remainder + print("binarnie calosc : ", message_final) + #konwersja z ascii do binarnego + ab=toString(message_final) + print("w ascii : ", ab) + print("sprawdzenie : ") + decode(message_final, poly16) @@ -76,6 +88,5 @@ poly16 = "10001000000100001" print("podaj wiadomosc do zakodowania : ") message = str(raw_input()) encode(message, poly16) -print("podaj wiadomosc do odkodowania : ") -message2 = str(raw_input()) -decode(message2, poly16) + +