-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarypy.py
More file actions
49 lines (46 loc) · 2.51 KB
/
binarypy.py
File metadata and controls
49 lines (46 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#Binary Encoder/Decoder
binary_dict={
'A': '01000001', 'B': '01000010', 'C': '01000011', 'D': '01000100', 'E': '01000101',
'F': '01000110', 'G': '01000111', 'H': '01001000', 'I': '01001001', 'J': '01001010',
'K': '01001011', 'L': '01001100', 'M': '01001101', 'N': '01001110', 'O': '01001111',
'P': '01010000', 'Q': '01010001', 'R': '01010010', 'S': '01010011', 'T': '01010100',
'U': '01010101', 'V': '01010110', 'W': '01010111', 'X': '01011000', 'Y': '01011001',
'Z': '01011010', 'a': '01100001', 'b': '01100010', 'c': '01100011', 'd': '01100100', 'e': '01100101',
'f': '01100110', 'g': '01100111', 'h': '01101000', 'i': '01101001', 'j': '01101010',
'k': '01101011', 'l': '01101100', 'm': '01101101', 'n': '01101110', 'o': '01101111',
'p': '01110000', 'q': '01110001', 'r': '01110010', 's': '01110011', 't': '01110100',
'u': '01110101', 'v': '01110110', 'w': '01110111', 'x': '01111000', 'y': '01111001',
'z': '01111010', '0': '00110000', '1': '00110001', '2': '00110010', '3': '00110011', '4': '00110100',
'5': '00110101', '6': '00110110', '7': '00110111', '8': '00111000', '9': '00111001',
'.': '00101110', ',': '00101100', '!': '00100001', '?': '00111111',
':': '00111010', ';': '00111011', '-': '00101101', '_': '01011111', '(': '00101000',
')': '00101001', '[': '01011011', ']': '01011101', '{': '01111011', '}': '01111101',
'"': '00100010', "'": '00100111', '@': '01000000', '#': '00100011', '$': '00100100',
'%': '00100101', '^': '01011110', '&': '00100110', '*': '00101010', '+': '00101011',
'=': '00111101', '/': '00101111', '\\': '01011100', '<': '00111100', '>': '00111110'
}
reverse_binary_dict = {v:k for k,v in binary_dict.items()}
mode=input("Type 'encode' to convert text to binary or 'decode' to convert binary to text")
if mode=='encode':
text_input=input("Enter Text:")
binary_output=""
for char in text_input:
if char in binary_dict:
binary_output += binary_dict[char]
elif char== " ":
binary_output += " "
else:
binary_output += "?"
print("Binary: ",binary_output)
elif mode=='decode':
binary_input = input("Enter Binary: ")
text_output = ""
i=0
while i+8 <= len(binary_input):
binary_chunk=binary_input[i:i+8]
if binary_chunk in reverse_binary_dict:
text_output += reverse_binary_dict[binary_chunk]
else:
text_output += "?"
i+=8
print("Decoded Text:",text_output)