-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumeric_conversion.py
More file actions
91 lines (77 loc) · 2.84 KB
/
numeric_conversion.py
File metadata and controls
91 lines (77 loc) · 2.84 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def hex_char_decode(digit):
digit = digit.lower()
hex_chars = "0123456789abcdef"
if digit in hex_chars:
return hex_chars.index(digit)
else:
return None
def hex_string_decode(hex_string):
hex_string = hex_string.lower().lstrip("0x")
decimal_value = 0
for digit in hex_string:
digit_value = hex_char_decode(digit)
if digit_value is not None:
decimal_value = decimal_value * 16 + digit_value
else:
return None
return decimal_value
def binary_string_decode(binary_string):
binary_string = binary_string.lstrip("0b") # Remove the "0b" prefix if present
decimal_value = 0
for digit in binary_string:
if digit == '0':
decimal_value = decimal_value * 2
elif digit == '1':
decimal_value = decimal_value * 2 + 1
else:
return None
return decimal_value
def binary_to_hex(binary_string):
decimal_value = binary_string_decode(binary_string)
if decimal_value is not None:
hex_result = hex(decimal_value).rstrip("L").lstrip("0x")
return hex_result.upper().zfill((len(hex_result) + 3) // 4 * 4)[1:]
else:
return None
if __name__ == '__main__':
while True:
print("Decoding Menu")
print("-------------")
print("1. Decode hexadecimal")
print("2. Decode binary")
print("3. Convert binary to hexadecimal")
print("4. Quit")
print()
choice = input("Please enter an option: ")
if choice == "1":
hex_string = input("Please enter the numeric string to convert: ")
decoded_value = hex_string_decode(hex_string)
if decoded_value is not None:
print(f"Result: {decoded_value}")
print()
else:
print("Invalid hexadecimal input.")
print()
elif choice == "2":
binary_string = input("Please enter the numeric string to convert: ")
decoded_value = binary_string_decode(binary_string)
if decoded_value is not None:
print(f"Result: {decoded_value}")
print()
else:
print("Invalid binary input.")
print()
elif choice == "3":
binary_string = input("Please enter the numeric string to convert: ")
hex_result = binary_to_hex(binary_string)
if hex_result is not None:
print(f"Result: {hex_result}")
print()
else:
print("Invalid binary input.")
print()
elif choice == "4":
print("Goodbye!")
break
else:
print("Invalid choice. Please select a valid option.")