-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathEX8.9.py
40 lines (35 loc) · 985 Bytes
/
EX8.9.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
# 8.9 (Binary to hex) Write a function that parses a binary number into a hex number.
# The function header is:
# def binaryToHex(binaryValue):
# Write a test program that prompts the user to enter a binary number and displays
# the corresponding hexadecimal value.
def binaryToHex(binaryString):
bin = binaryString[::-1]
hex = ''
while len(bin) > 0:
b = bin[0:4]
dec = 0
for i in range(len(b)):
dec = dec + int(b[i]) * 2 ** i
if 0 <= dec <= 9:
hex += str(dec)
elif dec == 10:
hex += 'A'
elif dec == 11:
hex += 'B'
elif dec == 12:
hex += 'C'
elif dec == 13:
hex += 'D'
elif dec == 14:
hex += 'E'
elif dec == 15:
hex += 'C'
bin = bin[4:len(bin)]
hex = hex[::-1]
return hex
def main():
bin = input("Enter a binary string: ")
hex = binaryToHex(bin)
print(hex)
main()