Conversion between Python's different binaries

Source: Internet
Author: User

Different binary

Binary 0b101

A binary number that starts with the number 0 and the letter B will throw a SyntaxError exception if a number greater than or equal to 2 appears

Octal 0711

Numbers preceded by number 0 indicate octal numbers if a number greater than or equal to 8 throws a SyntaxError exception

Decimal 123

Normal display cannot appear letter

Hex 0x15

The hexadecimal number that begins with the number 0 and subtitle x can occur when 0-9 and abcdef or abcdef other values throw a SyntaxError exception

Python built-in functions after 2.6

#10进制转为2进制

>>> Bin (Ten) ' 0b1010 '

#2进制转为10进制

>>> Int ("1001", 2) 9

#10进制转为16进制

>>> Hex ' 0xa '

#16进制到10进制

>>> int (' FF ', 16) 255
>>> int (' 0xAB ', 16) 171

#十进制转为八进制

>>print ("%o"%) >>12

#16进制到2进制

>>> bin (0xa) ' 0b1010 ' >>>

#10进制到8进制

>>> Oct (8) ' 010 '

#2进制到16进制

>>> Hex (0b1001) ' 0x9 '

In addition, in an interactive interpreter environment. Python automatically converts the different binaries to decimal.

>>> 0b101 + 0711 + 123 + 0x15 606
  • 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.