Use the Python built-in functions: Bin (), Oct (), int (), Hex () to enable a binary conversion.
Let's look at the description of these built-in functions in the official Python document:
bin (x)
Convert an integer number to a binary string. The result is a valid Python expression. If X was not a Python int object, it had to define an __index__ () method, which returns an integer.
Oct (x)
Convert an integer number to an octal string. The result is a valid Python expression. If X was not a Python int object, it had to define an __index__ () method, which returns an integer.
int ([number | string[, Base]])
Convert a number or string to an integer. If no arguments is given, return 0. If A number is given, return number.__int__ (). Conversion of floating point numbers to integers truncates towards zero. A string must is 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 ') has the values of 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 the integer literals in code. Base 0 means to interpret exactly as a code literal, so then the actual base is 2, 8, ten, or, and so that int (' 010 ', 0) Is isn't legal, while INT (' 010 ') are, 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 was not a Python int object, it had to define an __index__ () method, which returns an integer.
↓ |
2 binary |
8 binary |
10 binary |
16 binary |
2 binary |
- |
Bin (int (x, 8)) |
Bin (int (x, 10)) |
Bin (int (x, 16)) |
8 binary |
Oct (int (x, 2)) |
- |
Oct (int (x, 10)) |
Oct (int (x, 16)) |
10 binary |
int (x, 2) |
int (x, 8) |
- |
int (x, 16) |
16 binary |
Hex (int (x, 2)) |
Hex (int (x, 8)) |
Hex (int (x, 10)) |
- |
The return values for Bin (), Oct (), and Hex () are all strings, with 0b, 0o, and 0x prefixes respectively.