the
string.decode('hex')
method goes in the opposite direction. You should use the
hex function[
^] instead. There is one caveat, though, to zero-fill it, you have to remove (and restore it afterwards) the leading
'0x'
. Try
length=10;
s = '0x' + hex(length)[2:].zfill(2)
print(s)
[update]
Both
length=10;
s = '\\x' + hex(length)[2:].upper().zfill(2)
print(s)
and (the possibly clearer)
length=10
s="\\x{0:02X}".format(length)
print(s)
produce
\x0A
[/update]