SoFunction
Updated on 2024-11-10

What are the common number system conversions in Python?

Number system conversion, that is, conversion, refers to the system (binary, octal, decimal, hexadecimal) conversion between each other, more common in computer programming. Here is a list of common python conversion usage.

1. Progression system

Binary in Python starts with 0b:.

For example: 0b11 means 3 in decimal.

Hexadecimal starts with a 0.

For example: 011 is a decimal 9.

Hexadecimal starts with 0x.

For example: 0x11 means 17 in decimal.

Or write it as \x \b

2. Various function conversions

#10 to binary
>>> bin(10)
'0b1010'
#Binary to decimal
>>> int("1001",2)
9
#10 to hexadecimal
>>> hex(10)
'0xa'
# Hexadecimal to decimal
>>> int('ff', 16)
255
>>> int('0xab', 16)
171
# Decimal to octal
>>print("%o" % 10)
>>12
# Hexadecimal to binary
>>> bin(0xa)
'0b1010'
>>>
#10 to 8
>>> oct(8)
'010'
#Binary to hexadecimal
>>> hex(0b1001)
'0x9'

Expanded knowledge of common representations of the progression system:

>>> 0o1, 0o20, 0o377      # Commonly used octal representation, starts with 0o or 0o followed by a number
(1, 16, 255)
 
>>> 0x01, 0x10, 0xFF      # Common hexadecimal representation, starts with 0x or 0X, followed by 0-9,A-F.
(1, 16, 255)
 
>>> 0b1, 0b10000, 0b11111111  # Common binary representation, starts with 0b followed by a number consisting of 01
(1, 16, 255)

to this article on the Python common number system conversion which article is introduced to this, more relevant Python common number system conversion examples of content, please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future more!