如何在Python中将十六进制转换为字符串?

sda*_*das 2 python hex

使用Python,

h = 0x11012
# ... ???
result = '11012'
Run Code Online (Sandbox Code Playgroud)

从h - >结果我需要采取哪些中间步骤?

Ale*_*x L 9

Python 2.7及更高版本:

>>> "{:x}".format(0x11012)
'11012'
Run Code Online (Sandbox Code Playgroud)

Python 2.6:

>>> "{0:x}".format(0x11012)
'11012'
Run Code Online (Sandbox Code Playgroud)

Python 2.5及更早版本:

>>> "%x" % 0x11012
'11012'
Run Code Online (Sandbox Code Playgroud)

  • 这比看起来有点任意的`[2:]`切片更好. (2认同)