-
Notifications
You must be signed in to change notification settings - Fork 29
/
convert_from_int_to_hex.py
36 lines (29 loc) · 996 Bytes
/
convert_from_int_to_hex.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
from math import trunc
numbers = list(map(str, range(10)))
numbers += ["A", "B", "C", "D", "E", "F"]
list_numbers_hex = list(enumerate(numbers))
def from_int_to_hex(int_number):
"""
Convert an integer number to hexadecimal
use recursion
"""
if int_number < 16:
return list(filter(lambda e: e[0] == int_number, list_numbers_hex))[0][1]
while int_number >= 16:
return from_int_to_hex(trunc(int_number / 16)) + from_int_to_hex(
int_number % 16
)
def aux_validate_int(txt_int_number):
"""
Validate an int number
"""
txt_int_number = txt_int_number.strip().upper()
if str(txt_int_number).isdigit():
return int(txt_int_number)
return False
if __name__ == "__main__":
print("-" * 50)
while not (int_number := aux_validate_int(input("Informe an integer number: "))):
print("NUMBERS only! [0-9]")
print(f"{int_number}(int) to hex is: {from_int_to_hex(int_number)}")
print("-" * 50)