binary conversion: First introduce the method without function, then introduce the method of using functionBinary goto decimal:1101 to decimal 1*2^ (4-1) +1*2^ (3-1) +0*2^ (2-1) +1*2^ (1-1) Each bit is split, multiplied by 2 (number of digits-1), the result is 13
>>> int ('1101', 2) 13
>>> int (' 0o226 ', 8) #00226 0: Arabic numerals 0 o: lowercase English letter o 226: Octal number
150
>>> int (' 0x96 ', 16)
150
The same can be done: the octal, hexadecimal number by the right to expand, add the decimal number.
decimal Turn binary:For example, 13 is divided by 13 by 2, the quotient is divided by 2 until it is 1, and then the remainder is recorded each time, from backward to forward reading, that is 1101In the same way , decimal to octal or hexadecimal according to the addition of 8 or 16 to the remainder, until the quotient is 0, and then from the last one to read the first bit
>>> bin (+)'0b1101'
octal into binary:For example, octal number (37) 8 split 3 7 3 with 2 prohibitions expressed as 11 7 in binary notation for 111 together that is 11 111similarly hexadecimal to binary binary
>>> bin (0o37) #0o37 0: Arabic numerals 0 o: octal notation '0b11111' >>> bin (0x33)'0b110011'
Binary turn octal:Binary from the right number, 3 bits 3 bits for a group, to decimal such as: 10 110 011 1112 6 3 7 corresponding octal is (2637) 8In the same way hexadecimal, but at this time the binary should be from right to left number, 4 bits 4 bits for a group
>>> Oct (0b10110011111)'0o2637'>>> hex (0b10110011111) '0x59f'
101 1001 1111 to hexadecimal corresponds to the following:
5 9 F (15)
There are two ways to convert octal to hexadecimal:
The first: the transitions between them can be turned into binary and then converted to each other.
The second is that the conversions between them can be converted into decimal and then converted to each other.
>>> Oct (0x37)'0o67'>>> Hex (0o67)' 0x37'
Python binary conversion