3

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,

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
Aditya Singh
  • 822
  • 3
  • 15
  • 25

2 Answers2

5

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'
Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
0

python2php web site can help you - it suggest using struct.pack

Maxim Krizhanovsky
  • 26,265
  • 5
  • 59
  • 89