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

Python struct reverse unpack

I’m trying to unpack structure as shown below in this way to get ‘0123456789’

b"\x10\x32\x54\x76\x98"

Maybe there is other way then writing own function to do so ?

Thanks

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 :

I think it’s easier to debug if you use your own function. However, if you want a one line command to turn this into a string you can do like so:

X = b"\x10\x32\x54\x76\x98"

tmp = "".join([hex(i)[:1:-1] for i in X])

print(tmp)

>>> 0123456789

Basically it turns each byte to hexadecimal (which handles splitting methods), so we split them to read them backward without "0x".

Finally we join them using an empty string separator.

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