s2k s2k - 2 months ago 8
Python Question

How to convert int to 4 byte hex

I need to convert an unsigned integer like 4003 to its hex representation. The output of the following code

print(struct.pack("<I", 4003).encode('hex'))


is


a30f0000


How can I get the following output?


00000fa3


It's not necessary to use
struct.pack
. Any other approach would be appreciated.

Answer
>>> '{:08x}'.format(4003)
'00000fa3'