1

我有一个整数(67)需要转换为十六进制并存储为字符串,如:

"\x00\x00\x00\x43"

我怎样才能在 Python 中做到这一点?

4

1 回答 1

1

由于 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
于 2013-04-10T19:14:34.797 回答