Home > Net >  Converting hex to little endian in python
Converting hex to little endian in python

Time:10-12

I have a hex number which is represented like this

Hex num:

b'95b64fc1830100'

How could I converted to little endian so it could be shown like this?

00000183C14FB695    

CodePudding user response:

I think this does the trick (input is hex):

hex_num = b'95b64fc1830100'
big = bytearray.fromhex(str(hex_num, 'utf-8'))
big.reverse()
little = ''.join(f"{n:02X}" for n in big)
print(little)

This prints:

000183C14FB695

The output is a string, if you want to get the result back to bytes you can add the following lines:

hex_bytes= bytearray(int(little, 16).to_bytes(8, 'big')).hex()
print(hex_bytes)
  • Related