-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
63 lines (54 loc) · 1.33 KB
/
main.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
print("0:Exit\n")
print("1:Binary to decimal\n")
print("2:Decimal to binary\n")
print("3:Hexadecimal to binary\n")
print("4:Binary to text\n")
def binToDec(binary):
decimal = 0
exponent = 0
reversedBinary = binary[::-1]
for number in reversedBinary:
if number=="1":
decimal += 2**exponent
exponent+= 1
elif number=="0":
exponent+= 1
return decimal
def decToBin(decimal):
binary = str(bin(decimal))
formattedBinary = binary[2:]
return formattedBinary
def hexToBin(hexadecimal):
return format(int(hexadecimal, 16), "040b")
def binaryToText(binaryUser):
binaryArray = binaryUser.split()
decimal = 0
exponent = 0
text = ""
for binary in binaryArray:
for number in binary[::-1]:
if(number=="1"):
decimal += 2**exponent
exponent+= 1
elif(number=="0"):
exponent+= 1
text += chr(decimal)
return text
while True:
option = int(input("Pick an option: "))
if option==0:
break
elif option==1:
userBinary = str(input("Enter binary value: "))
print(binToDec(userBinary))
elif option==2:
userDecimal = int(input("Enter decimal value: "))
print(decToBin(userDecimal))
elif option==3:
userHex = str(input("Enter hexadecimal value: "))
print(hexToBin(userHex))
elif option==4:
userBinary = str(input("Enter binary value: "))
print(binaryToText(userBinary))
else:
print("Wrong input")