我有一个整数(67)
需要转换为十六进制并存储为字符串,如:
"\x00\x00\x00\x43"
我怎样才能在Python中执行此操作?
答案 0 :(得分:0)
由于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