>>> 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'
题
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'