我怎样才能解开在Python二进制十六进制格式的数据? [英] How can I unpack binary hex formatted data in Python?
问题描述
使用PHP 包()函数,我已经转换字符串转换成二进制十六进制再presentation:
Using the PHP pack() function, I have converted a string into a binary hex representation:
$string = md5(time); // 32 character length
$packed = pack('H*', $string);
的H *格式意味着十六进制字符串,高半字节先。
The H* formatting means "Hex string, high nibble first".
要在PHP解压这一点,我会简单地使用解包()与H *格式功能标志。
To unpack this in PHP, I would simply use the unpack() function with the H* format flag.
我怎么会在Python解压这些数据?
How would I unpack this data in Python?
推荐答案
有一个简单的方法使用 binascii
模块来做到这一点:
There's an easy way to do this with the binascii
module:
>>> import binascii
>>> print binascii.hexlify("ABCZ")
'4142435a'
除非我误解有关四位顺序的东西(高四位首先是默认值),这应该是完全足够了!
Unless I'm misunderstanding something about the nibble ordering (high-nibble first is the default), that should be perfectly sufficient!
此外,Python的 hashlib.md5
对象有一个 hexdigest()
方法来自动转换MD5摘要到的ASCII十六进制字符串,所以,这种方法甚至没有必要为MD5摘要。希望有所帮助。
Furthermore, Python's hashlib.md5
objects have a hexdigest()
method to automatically convert the MD5 digest to an ASCII hex string, so that this method isn't even necessary for MD5 digests. Hope that helps.
这篇关于我怎样才能解开在Python二进制十六进制格式的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!