Skip to content
Advertisement

Converting HEX string into int [closed]

I need help on converting the varbinary data which I acquired from sql, into int by taking 4 bits at a time. From the code above, I acquired the results as (‘0x640761075D075A0…..’). My plan is to take every 4 bits & swap it (ex: 0764 0761 07D5 and so on) and then turn every 4 bits into integers. what should I do? thank you very much!

Advertisement

Answer

Try it:

from struct import pack, unpack

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 [unpack('<H', pack('>H', int(i, 16)))[0] for i in splitted]

print(convert_hex_to_int(0x640761075D07, 4))

It will return list of int: [1892, 1889, 1885]

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement