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
| #! python3
# coding: utf-8
from math import trunc
# dict mapping dec -> hexa
D2H = dict(zip(range(16), "0123456789abcdef"))
def to_hex(n: int) -> str:
lst = []
while True:
if n >= 16:
quotient = trunc(n / 16)
reste = n % 16
lst.append(str(D2H[reste]))
n = quotient
else:
lst.append(str(D2H[n]))
break
# on a obtenu les éléments dans l'ordre inverse
lst.reverse()
return "0x" + "".join(lst)
numbers = [0, 11, 16, 32, 33, 41, 45, 678, 775, 1256, 574893]
print([to_hex(x) for x in numbers])
# debug
print([hex(x) for x in numbers])
'''
['0x0', '0xb', '0x10', '0x20', '0x21', '0x29', '0x2d', '0x2a6', '0x307', '0x4e8', '0x8c5ad']
['0x0', '0xb', '0x10', '0x20', '0x21', '0x29', '0x2d', '0x2a6', '0x307', '0x4e8', '0x8c5ad']
''' |