问题
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
, but the result is not the same.
回答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'
来源:https://stackoverflow.com/questions/13065863/pack-data-into-binary-string-in-python