使用Python中的填充将整数转换为十六进制

时间:2013-04-10 19:04:51

标签: python hex decimal base-conversion

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

"\x00\x00\x00\x43"

我怎样才能在Python中执行此操作?

1 个答案:

答案 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