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.
我有一个整数(67)需要转换为十六进制并存储为字符串,如:
(67)
"\x00\x00\x00\x43"
我怎样才能在 Python 中做到这一点?
由于 OP 中的歧义而更新。
尝试...
def convert(i): result = '' for c in struct.pack('>i', 67): c = hex(ord(c))[2:] if len(c) < 2: c = '0%s' % c result += '\\x%s' % c return result >>> print convert(67) \x00\x00\x00\x43