Easily convert between various binary numbers with Python built-in functions

Source: Internet
Author: User



0. Description






9 months did not write Python, this 9 months in the  ICT knowledge of the sea, the previous time just passed the Hcie certification, think of still like Python and Linux more, so back, there will be more and more python dry share to everyone, For example, later will be going to write a complete Linux host monitoring project of the tutorial to the beginning of the friend, I believe this will be a very good experience.



During that time, there were times when I needed to convert the various conversions, because although it would not be python, it would be handy to use Python's own interpreter for simple math calculations.



Find in the Internet, see there is no way to achieve a variety of conversion, and then find the results are all the users themselves to achieve a variety of algorithms, fortunately did not see it, in fact, Python built-in functions can be perfect to achieve a variety of conversion between the system, very practical, Think that the computer major must be these conversions.



Of course, the main discussion is the conversion between binary, octal, decimal, hexadecimal.









1. Convert decimal number to other binary number






(1) Decimal number converted to binary number



Implemented via the built-in function bin, for example:


>>> Bin (Ten) ' 0b1010 ' >>> bin (255) ' 0b11111111 ' >>> bin (65535) ' 0b1111111111111111 '





(2) Decimal number converted to octal number



Implemented by the built-in function Oct, examples are as follows:


>>> Oct (Ten) ' 0o12 ' >>> Oct (255) ' 0o377 ' >>> Oct (65535) ' 0o177777 '





(3) Decimal number converted to hexadecimal number



Implemented by the built-in function hex, examples are as follows:


>>> Hex (0xa) >>> Hex (255) ' 0xFF ' >>> hex (65535) ' 0xFFFF '








2. Convert other binary numbers to decimal numbers






Converting binary, octal, and hexadecimal numbers to decimal numbers is achieved through the built-in function int, except that the second parameter received by int is different, and can be viewed by Help (int) to see the parameter description that it receives:


>>> help (int) Help on class int in module builtins:class int ( Object)  |  int (x=0)  -> integer |  int (x, base=10)  ->  integer |   |  convert a number or string to  an integer, or return 0 if no arguments |  are  given.  if x is a number, return x.__int__ () .  For  floating point |  numbers, this truncates towards zero. |    |  If x is not a number or if base is  Given, then x must be a string, |  bytes, or bytearray  instance representing an integer literal in the |  given  base.  the literal can be preceded by  ' + '  or  '-'  and be surrounded  |  by whitespace.  The base defaults to 10.   valid bases are 0 and 2-36. |  base 0 means to  interpret the base from the string as an integer literal. |   >>> int (' 0b100 ',  base=0)  |  4


That is, the INT function also has a default parameter base, whose value defaults to 10, the conversion to a decimal number, and if 2, the conversion to a binary number, but it should be noted that if you need to display the specified base parameter, then the data type of the first parameter must be a string, you can see the following example:


>>> Int (1010,2) Traceback (most recent call last): File "<stdin>", line 1, in <module>typeerror:int ( ) can ' t convert non-string with explicit base





(1) binary number converted to decimal number



Examples are as follows:


>>> Int (' 1010 ', 2) 10>>> int (' 11111111 ', 2) 255>>> int (' 1111111111111111 ', 2) 65535





(2) Octal number converted to decimal number



Examples are as follows:


>>> int (' A ', 8) 10>>> int (' 377 ', 8) 255>>> int (' 177777 ', 8) 65535





(3) Hexadecimal number converted to decimal number



Examples are as follows:


>>> int (' a ', +) 10>>> int (' FF ', +) 255>>> int (' FFFF ', 16) 65535








3. Conversion of various binary numbers to each other






The above gives the situation is the decimal number and the conversion between the various binary, in fact, binary to octal or binary to hexadecimal is also possible, but still need to pass the decimal as a bridge to convert, the following examples.






(1) Binary number converted to octal number



For example, you need to convert the binary number 0b1010 to eight decimal digits.



Convert to decimal number first:


>>> Int (' 1010 ', 2) 10


Convert to octal number:


>>> Oct (Ten) ' 0o12 '


or one step:


>>> Oct (int (' 1010 ', 2)) ' 0o12 '





(2) Octal number converted to hexadecimal number



For example, you need to convert octal number 0o377 to hexadecimal number.



Convert to decimal number first:


>>> int (' 377 ', 8) 255


Convert to hexadecimal number:


>>> Hex (255) ' 0xFF '


or one step:


>>> Hex (int (' 377 ', 8)) ' 0xFF '








Of course, if you need octal to binary or other situations, is the same way of thinking, the whole process of thinking is very clear and concise.









4. What's the use?






It is also very useful to be used only as a calculation, and of course, it is entirely possible to use it in future project development.






This article is from the "fragrant fluttering leaves" blog, please make sure to keep this source http://xpleaf.blog.51cto.com/9315560/1890242



Easily convert between various binary numbers with Python built-in functions


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.