Python: Convert hex to string -
a value 0x0f
or 0x05
has given function string without 0x @ beginning 0f
or 05
.
if use str(hex(0x0f))[2:4]
f
. crucial missing 0
still present in application.
how can that?
you'd use str.format
:
in [1]: '{:02x}'.format(0x0f) out[1]: '0f'
in context {:02x}
equivalent {0:02x}
. preceding colon 0 tells python apply first argument of str.format
(0x0f in example). 02 sets minimum field width 2 ,
preceding width field 0 ('0') character enables sign-aware zero-padding numeric types
x 1 of available integer presentation types:
hex format. outputs number in base 16, using upper- case letters digits above 9.
both quotes format specification mini-language.
Comments
Post a Comment