Python built-in function bin () Oct () to implement a binary conversion

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

    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.