This article provides a detailed description of the Python built-in hex function in English:
Hex (x)
Convert an integer number to a lowercase hexadecimal string prefixed with "0x", for example
If x is not a Python int object, it has to define an index () method that returns an integer.
Note:
1. the Function converts a 10-digit integer to a 16-digit integer.
>>> hex(15)'0xf'>>> hex(16)'0x10'
2. if The x parameter is not an integer, it must define an index function that returns an integer.
# Undefined _ index _ function >>> class Student: def _ init _ (self, name, age): self. name = name self. age = age >>>>>> s = Student ('Kim ', 10) >>> hex (s) Traceback (most recent call last): File"
", Line 1, in
Hex (s) TypeError: 'Student 'object cannot be interpreted as an integer # defines the _ index _ function, but returns a string >>> class Student: def _ init _ (self, name, age): self. name = name self. age = age def _ index _ (self): return self. name >>> s = Student ('Kim ', 10) >>> hex (s) Traceback (most recent call last): File"
", Line 1, in
Hex (s) TypeError: _ index _ returned non-int (type str) # defines the _ index _ function and returns an integer >>> class Student: def _ init _ (self, name, age): self. name = name self. age = age def _ index _ (self): return self. age >>>> s = Student ('Kim ', 10) >>> hex (s) '0xa'
The above is a detailed description of the Python built-in hex function. For more information, see other related articles in the first PHP community!