Home:ALL Converter>Converting from HEX string to int without removing leading zeroes on Python

Converting from HEX string to int without removing leading zeroes on Python

Ask Time:2019-03-04T07:33:16         Author:Crn

Json Formatter

I need help on converting HEX string to int. I have a big data input, but, Here's one example from the data:

def convert_hex_to_int(n:int, interval:int):
        splitted = [hex(n)[2:][i:i + interval] for i in range(0, len(hex(n)[2:]), interval)]
        return [float(int(hex(unpack('<H', pack('>H', int(i, 16)))[0]), 16)) for i in splitted]

    a='0x0E070907'
hex_int = int(a, 16)
result_print = (convert_hex_to_int(hex_int, 4))

and instead of

[1806.0, 1801.0]

the result is

[28896.0, 1801.0]

the function convert_hex_to_int is to split the string into 2 bytes and swap it, with the interval of 4 bytes. And the purpose of the code it to acquire the floating points of the HEX String. I suspected it has to do the Python removing the first zero on the front of 070E, and instead, it becomes 70E0.

  • I have read some similar problems in Stackoverflow but still didn't work. And I'm new to python. Thanks for your help.

Author:Crn,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/54974878/converting-from-hex-string-to-int-without-removing-leading-zeroes-on-python
yy