I have a decimal and write it with
hex(dezimal)
to the hex number. But I miss the 0, when i format digits 0-9
it should look like 0x09
and it looks 0x9
What can I do?
Thank you!
I have a decimal and write it with
hex(dezimal)
to the hex number. But I miss the 0, when i format digits 0-9
it should look like 0x09
and it looks 0x9
What can I do?
Thank you!
You had to modify the result by your own function:
def pad_hex(s):
return '0x' + s[2:].zfill(2)
print(pad_hex(hex(9)))
Output: 0x09
If you need only to output a single byte (i.e. values from 0..255),
"{0:#04x}".format(dezimal)
gives hex output with 0x prefix.
The field length (4) applies to the entire output, i.e. including the 0x prefix. If you need variable length, then @atline's solution is more flexible.