Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python equivalent of php pack

I have this statement below in php to convert hex string to binary.

$m=pack("H*" , "A88BE9L98990........");

I need to do the same in another python program ?

Any Ideas ?

Cheers,

like image 823
Aditya Singh Avatar asked Dec 17 '25 14:12

Aditya Singh


1 Answers

The binascii module has binascii.unhexlify(hexstr), which does what you want.

>>> import binascii
>>> binascii.unhexlify("A88BE9L98990")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Non-hexadecimal digit found
# Not sure why there's an L in there... take it out...
>>> binascii.unhexlify("A88BE9989900")
'\xa8\x8b\xe9\x98\x99\x00'
like image 193
Ned Batchelder Avatar answered Dec 20 '25 05:12

Ned Batchelder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!