Python built-in functions such as bin () oct () implement hexadecimal conversion

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.