Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
有谁知道如何获取任意长的十六进制字符串(例如"01020304deadbeef")并获取相应的字节("\x01\x02\x03\x04\xde\xad\xbe\xef")?在 Perl 中,这可以使用 获得pack('H*', $string),但我正在寻找 Python 解决方案。
"01020304deadbeef"
"\x01\x02\x03\x04\xde\xad\xbe\xef"
pack('H*', $string)
>>> T = (1, 2, 3) >>> struct.pack('H' * len(T), *T) '\x01\x00\x02\x00\x03\x00'
编辑:
>>> "01020304deadbeef".decode('hex') '\x01\x02\x03\x04\xde\xad\xbe\xef'