Does anybody know how to take an arbitrary long hex string (e.g. "01020304deadbeef") and obtain the corresponding bytes ("\x01\x02\x03\x04\xde\xad\xbe\xef")? In Perl, this can be obtained using pack('H*', $string), but I'm looking for a Python solution.

有帮助吗?

解决方案

>>> T = (1, 2, 3)
>>> struct.pack('H' * len(T), *T)
'\x01\x00\x02\x00\x03\x00'

EDIT:

>>> "01020304deadbeef".decode('hex')
'\x01\x02\x03\x04\xde\xad\xbe\xef'
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top