Use Python built-in functions: bin (), oct (), int (), and hex () to implement hexadecimal conversion. First, read the descriptions of these built-in functions in the Python official documentation, for more information, see The Python built-in functions bin (), oct (), int (), and hex.
First, let's take a look at the descriptions of these built-in functions in the Python official documentation:
Bin (x)
Convert an integer number to a binary string. the result is a valid Python expression. if x is not a Python int object, it has to define an _ index _ () method that returns an integer.
Oct (x)
Convert an integer number to an octal string. the result is a valid Python expression. if x is not a Python int object, it has to define an _ index _ () method that returns an integer.
Int ([number | string [, base])
Convert a number or string to an integer. if no arguments are given, return 0. if a number is given, return number. _ int __(). conversion of floating point numbers to integers truncates towards zero. A string must be a base-radix integer literal optionally preceded by '+' or '-' (with no space in between) and optionally surrounded by whitespace. A base-n literal consists of the digits 0 to n-1, with 'A' to 'Z' (or 'a' to 'Z') having values 10 to 35. the default base is 10. the allowed values are 0 and 2-36. base-2,-8, and-16 literals can be optionally prefixed with 0b/0B, 0o/0O, or 0x/0X, as with integer literals in code. base 0 means to interpret exactly as a code literal, so that the actual base is 2, 8, 10, or 16, and so that int ('010 ', 0) is not legal, while int ('010 ') is, as well as int ('010', 8 ).
Hex (x)
Convert an integer number to a hexadecimal string. the result is a valid Python expression. if x is not a Python int object, it has to define an _ index _ () method that returns an integer.
Bytes |
Binary |
Octal |
Decimal |
Hexadecimal |
Binary |
- |
Bin (int (x, 8 )) |
Bin (int (x, 10 )) |
Bin (int (x, 16 )) |
Octal |
Oct (int (x, 2 )) |
- |
Oct (int (x, 10 )) |
Oct (int (x, 16 )) |
Decimal |
Int (x, 2) |
Int (x, 8) |
- |
Int (x, 16) |
Hexadecimal |
Hex (int (x, 2 )) |
Hex (int (x, 8 )) |
Hex (int (x, 10 )) |
- |
The return values of bin (), oct (), and hex () are strings with prefix 0b, 0o, and 0x respectively.