Python binary, decimal, 16 binary conversion

Source: Internet
Author: User
Tags binary to decimal

hexadecimal to decimal

Using the Int () function, the first argument is the string ' 0Xff ', the second argument is the description, and the string is a number of binary numbers. The result of the conversion is a decimal number.

>>> int (' 0xf ', 16)
15

Binary to Decimal

>>> int (' 10100111110 ', 2)
1342

Octal to Decimal

>>> Int (' 17 ', 8)
15

You can actually see that the int () function is used regardless of the number of decimal numbers that are converted into decimals. After that, the second argument is written to clarify that the preceding string is a few decimal digits. Attention must be legal. For example, 2 binary number can not appear 2 such characters.

--------------------------------------------------------------------------------------------------------------- -------------

Decimal Turn hex

>>> Hex (1033)
' 0x409 '


Binary Turn hex

That is, binary first turns into a decimal, and then turns into 16 binary.

>>> Hex (int (' 101010 ', 2))
' 0X2A '

octal to hexadecimal

is octal first turn into decimal, then turn into 16 binary.

>>> Hex (int (' 17 ', 8))
' 0xf '

--------------------------------------------------------------------------------------------------------------- -------------

Decimal Turn binary

>>> Bin (10)
' 0b1010 '

Hex Turn Binary

hexadecimal--decimal--binary

>>> bin (int (' FF ', 16))
' 0b11111111 '

Octal binary to Binary

Octal first to decimal, then to binary

>>> bin (int (' 17 ', 8))
' 0b1111 '


--------------------------------------------------------------------------------------------------------------- -------------

Binary to octal

>>> Oct (0b1010)
' 012 '

Decimal to Octal

>>> Oct (11)
' 013 '

hexadecimal to octal

>>> Oct (0xf)
' 017 '

The OCT function can be seen to convert any number of bytes into a 8-based binary.

Python binary, decimal, 16 binary conversion

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.