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
.
python中的十六進制()函數,將字符0x放在數字前面。有什么可以告訴它不要放它們嗎?0xfa230就是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')
101
>>> format(3735928559, 'x')
'deadbeef'
36
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
對於Python 2.6使用
'{0:x}'.format(int(line))
or
或
'{0:06x}'.format(int(line))
9
You can simply write
你可以簡單地寫
hex(x)[2:]
to get the first two characters removed.
刪除前兩個字符。
4
Old style string formatting:
老樣式字符串格式:
In [3]: "%02x" % 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.
文檔都在這里。
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2013/05/07/725b82bb378c3ff81745826805ff2b18.html。