Starting with Python 3.6, you can:
>>> value = 42
>>> padding = 6
>>> f"{value:#0{padding}x}"
'0x002a'
Note the padding includes the 0x. If you don't want that you can do
>>> f"0x{value:0{padding}x}"
'0x00002a'
for older python versions use the .format() string method:
>>> "{0:#0{1}x}".format(42,6)
'0x002a'
Explanation:
{ # Format identifier
0: # first parameter
# # use "0x" prefix
0 # fill with zeroes
{1} # to a length of n characters (including 0x), defined by the second parameter
x # hexadecimal number, using lowercase letters for a-f
} # End of format identifier
If you want the letter hex digits uppercase but the prefix with a lowercase 'x', you'll need a slight workaround:
>>> '0x{0:0{1}X}'.format(42,4)
'0x002A'
Answer from Tim Pietzcker on Stack OverflowStarting with Python 3.6, you can:
>>> value = 42
>>> padding = 6
>>> f"{value:#0{padding}x}"
'0x002a'
Note the padding includes the 0x. If you don't want that you can do
>>> f"0x{value:0{padding}x}"
'0x00002a'
for older python versions use the .format() string method:
>>> "{0:#0{1}x}".format(42,6)
'0x002a'
Explanation:
{ # Format identifier
0: # first parameter
# # use "0x" prefix
0 # fill with zeroes
{1} # to a length of n characters (including 0x), defined by the second parameter
x # hexadecimal number, using lowercase letters for a-f
} # End of format identifier
If you want the letter hex digits uppercase but the prefix with a lowercase 'x', you'll need a slight workaround:
>>> '0x{0:0{1}X}'.format(42,4)
'0x002A'
If you don't need to handle negative numbers, you can do
"{:02x}".format(7) # '07'
"{:02x}".format(27) # '1b'
Where
:is the start of the formatting specification for the first argument{}to.format()02means "pad the input from the left with0s to length2"xmeans "format as hex with lowercase letters"
You can also do this with f-strings:
f"{7:02x}" # '07'
f"{27:02x}" # '1b'
hex(0) as 0x00?
Python , Printing Hex removes first 0? - Stack Overflow
python - Padding a hexadecimal string with zeros - Code Review Stack Exchange
How can I get the "format" function to leave my leading ZEROs in place?
in python3 hex(0) will return 0x0. Is there any way to make it return 0x00 ?
This is happening because hex() will not include any leading zeros, for example:
>>> hex(15)[2:]
'f'
To make sure you always get two characters, you can use str.zfill() to add a leading zero when necessary:
>>> hex(15)[2:].zfill(2)
'0f'
Here is what it would look like in your code:
fc = '0x'
for i in b[0x15c:0x15f]:
fc += hex(ord(i))[2:].zfill(2)
>>> map("{:02x}".format, (10, 13, 15))
['0a', '0d', '0f']
Perhaps you're looking for the .zfill method on strings. From the docs:
Help on built-in function zfill: zfill(...) S.zfill(width) -> string Pad a numeric string S with zeros on the left, to fill a field of the specified width. The string S is never truncated.
Your code can be written as:
def padhexa(s):
return '0x' + s[2:].zfill(8)
assert '0x00000123' == padhexa('0x123')
assert '0x00ABCD12' == padhexa('0xABCD12')
assert '0x12345678' == padhexa('0x12345678')
I would suggest interpreting the input as a number, then using standard number-formatting routines.
padded = str.format('0x{:08X}', int(mystring, 16))
The string → int → string round trip may seem silly, but it is also beneficial in that it provides validation of the input string.
(Recommended)
Python 3 f-strings: Answered by @GringoSuave
>>> i = 3735928559
>>> f'{i:x}'
'deadbeef'
Alternatives:
format builtin function (good for single values only)
>>> format(3735928559, 'x')
'deadbeef'
And sometimes we still may need to use str.format formatting in certain situations @Eumiro
(Though I would still recommend f-strings in most situations)
>>> '{:x}'.format(3735928559)
'deadbeef'
(Legacy) f-strings should solve all of your needs, but printf-style formatting is what we used to do @msvalkon
>>> '%x' % 3735928559
'deadbeef'
Without string formatting @jsbueno
>>> i = 3735928559
>>> i.to_bytes(4, "big").hex()
'deadbeef'
Hacky Answers (avoid)
hex(i)[2:] @GuillaumeLemaître
>>> i = 3735928559
>>> hex(i)[2:]
'deadbeef'
This relies on string slicing instead of using a function / method made specifically for formatting as hex. This is why it may give unexpected output for negative numbers:
>>> i = -3735928559
>>> hex(i)[2:]
'xdeadbeef'
>>> f'{i:x}'
'-deadbeef'
Use this code:
'{:x}'.format(int(line))
it allows you to specify a number of digits too:
'{:06x}'.format(123)
# '00007b'
For Python 2.6 use
'{0:x}'.format(int(line))
or
'{0:06x}'.format(int(line))