Python built in functions such as bin of oct of implement the conversion of base

  • 2020-04-02 09:47:04
  • OfStack

Hexes can be converted using Python built-in functions: bin(), oct(), int(), hex().
Let's start with the description of these built-in functions in Python's 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 arbitration 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 exponential () 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, The return number. __int__ (). Conversion of floating point Numbers to integers truncates forward zero. A string must be A base - radix integer literal optionally preceded by A '+' or '-' (with no space in Between) and optionally surrounded by whitespace. A base - n literal consists of the who 0 to n - 1, With 'a' to 'z' (or 'a' to 'z') having 10 to 35 values. The default base is 10. The allowed values are 0 and 2-36. The base - 2, 8, and - 16 literals can be optionally prefixed with 0 b / 0 b, 0 o/o, / 0 x or 0 x, 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 exponential () method that returns an integer.
left 2 base 8 hexadecimal decimal hexadecimal 2 base - Bin (int) (x, 8) Bin (int (x, 10)) Bin (int (x, 16)) 8 hexadecimal 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 prefixes of 0b, 0o and 0x, respectively.

Related articles: