Manual
Convert an integer number to a lowercase hexadecimal string prefixed with “0x”, for example:
>>>
>>> hex(255)
'0xff'
>>> hex(-42)
'-0x2a'
If x is not a Python int object, it has to define an __index__() method that returns an integer.
See also int() for converting a hexadecimal string to an integer using a base of 16.
Note To obtain a hexadecimal string representation for a float, use the float.hex() method.
直译
将整数转化为小写十六进制字符串,前缀“0x”,例如:
>>>
>>> hex(255)
'0xff'
>>> hex(-42)
'-0x2a'
如果x不是Python整数对象,那么它必须定义一个__index__()方法以便返回一个整数。
同理参考int(),将十六进制字符串转化为整数。
注意:十六进制字符串表述的浮点数,需要使用float.hex()方法。