How to convert character to hexadecimal and back?
Hex string variable to hex value conversion in python - Stack Overflow
How to use hex() without 0x in Python? - Stack Overflow
python - Decorating Hex function to pad zeros - Stack Overflow
I think you might be mixing up numbers and their representations. 0x01234567 and 19088743 are the exact same thing. "0x01234567" and "19088743" are not (note the quotes).
To go from a string of hexadecimal characters, to an integer, use int(value, 16).
To go from an integer, to a string that represents that number in hex, use hex(value).
>>> a = 0x01234567
>>> b = 19088743
>>> a == b
True
>>> hex(b)
'0x1234567'
>>> int('01234567', 16)
19088743
>>>
>>> int('01234567', 16)
19088743
This is the same as:
>>> 0x01234567
19088743
(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))
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'
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'
in python3 hex(0) will return 0x0. Is there any way to make it return 0x00 ?