简体   繁体   English

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

[英]How do I convert a hexadecimal to a string in Python?

Using Python, 使用Python,

h = 0x11012
# ... ???
result = '11012'

What intermediary steps do I have to take to go from h -> result? 从h - >结果我需要采取哪些中间步骤?

Python 2.7 and later: Python 2.7及更高版本:

>>> "{:x}".format(0x11012)
'11012'

Python 2.6: Python 2.6:

>>> "{0:x}".format(0x11012)
'11012'

Python 2.5 and earlier: Python 2.5及更早版本:

>>> "%x" % 0x11012
'11012'
h = 0x11012
result = hex(h)[2:]

This will remove the leading 0x . 这将删除前导0x

result = hex(h)

这真的很简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM