Skip to content Skip to sidebar Skip to footer

Pack Data Into Binary String In Python

Using the PHP pack() function, I have converted a string into a binary hex representation: pack('H*', $SECURE_SECRET) How can I get the same result in Python? I tried struct.pack,

Solution 1:

pack('H*', $value) converts hexadecimal numbers to binary:

php> = pack('H*', '41424344')
'ABCD'

In Python, you can use binascii.unhexlify to get the same result:

>>> from binascii import unhexlify
>>> unhexlify('41424344')
>>> 'ABCD'

Post a Comment for "Pack Data Into Binary String In Python"