-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
54 lines (47 loc) · 2.19 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
#####################################################################################################################
# Calculate the amount of letters inside the inputted binary.
def calcLenOfBin(binary):
convertToString = str(binary)
calcLenOfBin = len(convertToString)
return calcLenOfBin
# Conversion from binary to decimal to octal using python function oct.
def convertOctal(binary):
num = convertDecimal(binary)
return oct(num)
# Conversion from binary to decimal to hexadecimal using python function hexa.
def convertHexadecimal(binary):
num = convertDecimal(binary)
return hex(num)
# Conversion from binary to decimal using the base 2 of the python function int.
def convertDecimal(binary):
return int(binary, 2)
# Constructor
if __name__ == '__main__':
initiated = True
# While loop to ask for input and desired output.
while initiated:
binary = input("Enter the Binary: ")
lenOfBin = calcLenOfBin(binary)
if lenOfBin > 8:
print("ERROR: Input a max of 8 digits!")
continue
else:
print("Select what binary conversion you want: "
"\n1. Decimal"
"\n2. Hexadecimal"
"\n3. Octal"
"\n4. All")
selection = input("SELECTION: ")
if int(selection) == 1:
print("Conversation: ", convertDecimal(binary))
elif int(selection) == 2:
print("Conversation: ", str(convertHexadecimal(binary)).replace("0x", "").upper())
elif int(selection) == 3:
print("Conversation: ", convertOctal(binary))
elif int(selection) == 4:
print("The binary value of ", binary, "is:"
"\nDecimal: ", convertDecimal(binary),
"\nHexadecimal: ", str(convertHexadecimal(binary)).replace("0x", "").upper(),
"\nOctal: ", convertOctal(binary).replace("0o", "").upper())
initiated = False
#####################################################################################################################