(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'
Answer from jamylak on Stack Overflow(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))
hex - How to convert an integer to hexadecimal without the extra '0x' leading and 'L' trailing characters in Python? - Stack Overflow
python - Remove 0x from Hex String - Stack Overflow
python - How to turn decimals into hex without prefix `0x` - Stack Overflow
python - Decorating Hex function to pad zeros - Stack Overflow
How do I convert a decimal number to hexadecimal without 0x in Python?
Is there a universal method to remove the '0x' prefix for both Python 2 and 3?
Does Python 3.9+ offer an easier way to remove the '0x' prefix?
The 0x is literal representation of hex numbers. And L at the end means it is a Long integer.
If you just want a hex representation of the number as a string without 0x and L, you can use string formatting with %x.
>>> a = 44199528911754184119951207843369973680110397
>>> hex(a)
'0x1fb62bdc9e54b041e61857943271b44aafb3dL'
>>> b = '%x' % a
>>> b
'1fb62bdc9e54b041e61857943271b44aafb3d'
Sure, go ahead and remove them.
hex(bignum).rstrip("L").lstrip("0x") or "0"
(Went the strip() route so it'll still work if those extra characters happen to not be there.)
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'
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']