Through the code for i in range(2000,50000) I want to output the numbers from 2000 to 50000 in the following string format:
when i = 2000 output : 07 D0 When i = 50000 output : C3 50
How can I do that?
Through the code for i in range(2000,50000) I want to output the numbers from 2000 to 50000 in the following string format:
when i = 2000 output : 07 D0 When i = 50000 output : C3 50
How can I do that?
You can try this way:
def format_hex(number):
# convert to hex string
hex_str = hex(number)
# upper case
hex_str_upper = hex_str.upper()
# format
hex_value = hex_str_upper[2:]
if len(hex_value)==3:
hex_value = '0' + hex_value
hex_value_output = hex_value[:2] + ' ' + hex_value[2:]
return hex_value_output
Try:
def format_hex(i: int) -> str:
h = hex(i)[2:]
h = h.zfill(len(h) + len(h) % 2).upper()
return ' '.join(h[i:i+2] for i in range(0, len(h), 2))
Usage:
>>> print(format_hex(2000))
07 D0
>>> print(format_hex(50000))
C3 50