Pack data into binary string in Python [closed]

孤者浪人 提交于 2019-12-12 05:11:07

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!