格式化要用 2 位数字打印的十六进制?
Formatting hex to be printed with 2 digits?
python returns 中的内置函数 hex() 给我一个 0xf 或 0x0 形式的数字,但我希望它们为 0x00。我找到了 this on the python docs 但真的不知道如何解释它。
谢谢
您可以为此目的使用字符串格式:
>>> "0x{:02x}".format(13)
'0x0d'
这里有更详细的例子:
How can I format an integer to a two digit hex?
在 python3 中,使用这种 f
字符串格式:
f"0x{variable:02x}"
为了最大 python2/3 兼容性,请使用 .format()
:
"0x{:02x}".format(variable)
python returns 中的内置函数 hex() 给我一个 0xf 或 0x0 形式的数字,但我希望它们为 0x00。我找到了 this on the python docs 但真的不知道如何解释它。
谢谢
您可以为此目的使用字符串格式:
>>> "0x{:02x}".format(13)
'0x0d'
这里有更详细的例子:
How can I format an integer to a two digit hex?
在 python3 中,使用这种 f
字符串格式:
f"0x{variable:02x}"
为了最大 python2/3 兼容性,请使用 .format()
:
"0x{:02x}".format(variable)