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()方法。
本文介绍了Python内建函数hex(x),用于将整数转换为小写十六进制字符串,前缀为'0x'。当输入非整数时,需定义__index__()方法返回整数。同时提到了int()函数的逆过程,以及float.hex()方法用于表示浮点数的十六进制形式。
347

被折叠的 条评论
为什么被折叠?



