-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary Cipher.py
More file actions
47 lines (30 loc) · 1.12 KB
/
Binary Cipher.py
File metadata and controls
47 lines (30 loc) · 1.12 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
"""
Convert a message to binary
"""
def text_to_binary(message):
binary = ' '.join(format(ord(x), 'b') for x in message)
print('Your converted message is the following: \n', binary)
"""
Convert a binary message back to a normal message
"""
def binary_to_text(message):
new_message = ''.join([chr(int(s, 2)) for s in message.split()])
print('Your converted binary code is the following: \n', new_message)
"""
We ask the user to type his choice.
One choice is to convert a message to binary or vice versa.
After that, we ask for his message
"""
choice_of_user = int(input('Please choose \n'
'1.To convert a text to a binary code: \n'
'2.To convert a binary code to a text: \n'
))
message = input('Type the message you want to convert: ')
def Binary(message, choice_of_user):
if choice_of_user == 1:
text_to_binary(message)
elif choice_of_user == 2:
binary_to_text(message)
else:
print('Wrong choice! Please try again')
Binary(message, choice_of_user)