Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I am trying to convert an integer to a hexadecimal number according to the format

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading