Domanda

I have an integer (67) that needs to be converted to hex and stored as a string like:

"\x00\x00\x00\x43"

How can I do this in Python?

È stato utile?

Soluzione

Updated due to ambiguity in OP.

Try...

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
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top