forked from kalmar/DALGLI0
Zadanie3
Encode wygląda okej
This commit is contained in:
parent
65909526af
commit
2a39bdbcdf
65
Zadanie3
65
Zadanie3
@ -1,11 +1,43 @@
|
|||||||
from sys import argv
|
from sys import argv
|
||||||
|
|
||||||
def toString(binaryString):
|
|
||||||
return "".join([chr(int(binaryString[i:i+8],2)) for i in range(0,len(binaryString),8)])
|
|
||||||
|
|
||||||
def toBinary(string):
|
def toBinary(string):
|
||||||
return "".join([format(ord(char),'#010b')[2:] for char in string])
|
return "".join([format(ord(char),'#010b')[2:] for char in string])
|
||||||
|
|
||||||
|
def binnahex(var):
|
||||||
|
all = {"0000": "0",
|
||||||
|
"0001": "1",
|
||||||
|
"0010": "2",
|
||||||
|
"0011": "3",
|
||||||
|
"0100": "4",
|
||||||
|
"0101": "5",
|
||||||
|
"0110": "6",
|
||||||
|
"0111": "7",
|
||||||
|
"1000": "8",
|
||||||
|
"1001": "9",
|
||||||
|
"1010": "A",
|
||||||
|
"1011": "B",
|
||||||
|
"1100": "C",
|
||||||
|
"1101": "D",
|
||||||
|
"1110": "E",
|
||||||
|
"1111": "F"
|
||||||
|
}
|
||||||
|
i = 0
|
||||||
|
output = ""
|
||||||
|
|
||||||
|
while (len(var) % 4 != 0):
|
||||||
|
var = "0" + var
|
||||||
|
|
||||||
|
while (i < len(var)):
|
||||||
|
output = output + all[var[i:i + 4]]
|
||||||
|
i = i + 4
|
||||||
|
|
||||||
|
output = output.lstrip("0")
|
||||||
|
output = "0" if len(output) == 0 else output
|
||||||
|
|
||||||
|
return output
|
||||||
|
|
||||||
|
def toString(binaryString):
|
||||||
|
return "".join([chr(int(binaryString[i:i+8],2)) for i in range(0,len(binaryString),8)])
|
||||||
|
|
||||||
#XOR
|
#XOR
|
||||||
def xor(a, b):
|
def xor(a, b):
|
||||||
@ -73,20 +105,25 @@ def decode(message_final,poly16):
|
|||||||
def encode(message, poly16):
|
def encode(message, poly16):
|
||||||
#konwersja z ascii do binarnego
|
#konwersja z ascii do binarnego
|
||||||
aa=toBinary(message)
|
aa=toBinary(message)
|
||||||
|
a=toString(aa)
|
||||||
l_poly16 = len(poly16)
|
l_poly16 = len(poly16)
|
||||||
|
|
||||||
|
obroc = list(aa)
|
||||||
|
for i in range(l_poly16 - 1):
|
||||||
|
if obroc[i] == "0":
|
||||||
|
obroc[i] = "1"
|
||||||
|
elif obroc[i] == '1':
|
||||||
|
obroc[i] = "0"
|
||||||
|
|
||||||
|
aa = "".join(obroc)
|
||||||
|
|
||||||
# Dodajemy n-1 "0" do wiadomosci
|
# Dodajemy n-1 "0" do wiadomosci
|
||||||
appended_message = aa + '0'*(l_poly16-1)
|
appended_message = aa + '0'*(l_poly16-1)
|
||||||
remainder = divide(appended_message, poly16)
|
remainder = divide(appended_message, poly16)
|
||||||
|
ab=binnahex(remainder)
|
||||||
message_final = aa + remainder
|
message_final = a + ab
|
||||||
print("binarnie calosc : ", message_final)
|
print("calosc : ", message_final)
|
||||||
#konwersja z binarnego do ascii
|
print("crc : ", ab)
|
||||||
ab=toString(message_final)
|
|
||||||
print("w ascii : ", ab)
|
|
||||||
print("sprawdzenie : ")
|
|
||||||
decode(message_final, poly16)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user