The hex() function in python, puts the leading characters 0x in front of the number. Is there anyway to tell it NOT to put them? So 0xfa230 will be fa230.
The code is
import fileinput
f = open('hexa', 'w')
for line in fileinput.input(['pattern0.txt']):
f.write(hex(int(line)))
f.write('n')
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
(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'
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'
Method 2
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))
Method 3
You can simply write
hex(x)[2:]
to get the first two characters removed.
Method 4
Python 3.6+:
>>> i = 240
>>> f'{i:x}'
'f0'
Method 5
Old style string formatting:
In [3]: "%x" % 127 Out[3]: '7f'
New style
In [7]: '{:x}'.format(127)
Out[7]: '7f'
Using capital letters as format characters yields uppercase hexadecimal
In [8]: '{:X}'.format(127)
Out[8]: '7F'
Docs are here.
Method 6
F-strings
Python 3’s formatted literal strings (f-strings) support the Format Specification Mini-Language, which designates x for hexadecimal numbers. The output doesn’t include 0x.
So you can do this:
>>> f"{3735928559:x}"
'deadbeef'
See the spec for other bases like binary, octal, etc.
Edit: str.removeprefix
Since Python 3.9, there is now a str.removeprefix method, which allows you to write the following more obvious code:
>>> hexadecimal = hex(3735928559)
>>> hexadecimal.removeprefix('0x')
'deadbeef'
Not that this does NOT work for negative numbers ❌:
>>> negadecimal = hex(-3735928559)
>>> negadecimal.removeprefix('0x')
'-0xdeadbeef'
Method 7
'x' – Outputs the number in base 16, using lower-case letters for the digits above 9.
>>> format(3735928559, 'x') 'deadbeef'
'X' – Outputs the number in base 16, using upper-case letters for the digits above 9.
>>> format(3735928559, 'X') 'DEADBEEF'
You can find more information about that in Python’s documentation:
Method 8
While all of the previous answers will work, a lot of them have caveats like not being able to handle both positive and negative numbers or only work in Python 2 or 3. The version below works in both Python 2 and 3 and for positive and negative numbers:
Since Python returns a string hexadecimal value from hex() we can use string.replace to remove the 0x characters regardless of their position in the string (which is important since this differs for positive and negative numbers).
hexValue = hexValue.replace('0x','')
EDIT: wjandrea made a good point in that the above implementation doesn’t handle values that contain 0X instead of 0x, which can occur in int literals. With this use case in mind, you can use the following case-insensitive implementation for Python 2 and 3:
import re
hexValue = re.sub('0x', '', hexValue, flags=re.IGNORECASE)
Method 9
Decimal to Hexadecimal,
it worked
hex(number).lstrip("0x").rstrip("L")
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0