SoFunction
Updated on 2024-10-29

Conversion in Python in detail

Pytdon Conversion

Pytdon provides us with powerful built-in functions and ways to format numbers to achieve the function of conversion, which are described below.

Let's start by listing all the functions as follows:

binary octal decimal system hexadecimal
binary - bin(int(n,8)) bin(int(n,10)) bin(int(n,16))
octal oct(int(n,2)) - oct(int(n,10)) oct(int(n,16))
decimal system int(n,2)  int(n,8) - int(n,16)
hexadecimal hex(int(n,2)) hex(int(n,8)) hex(int(n,10)) -

Here's how to use each function one by one:

Converts binary to other binary:

Convert binary to octal: oct(int(n,2))

Input: 1010

Output: 0o12

Convert binary to decimal: int(n,2)

n=input()
print(int(n,2))

Input: 1010

Output: 10

Convert binary to hexadecimal: hex(int(n,2))

n=input()
print(hex(int(n,2)))

Input: 1010

Output: 0xa

Converts octal to other binary:

Convert from octal to binary: bin(int(n,8))

n=input()
print(bin(int(n,8)))

Input: 1010

Output: 0b1000001000

Convert from 8 to 10: int(n,8)

n=input()
print(int(n,8))

Input: 1010

Output: 520

Convert hexadecimal to octal: hex(int(n,16))

n=input()
print(hex(int(n,8)))

Input: 1010

Output: 0x208

Conversion of decimal to other binary:

Description: 10 conversion can be taken directly in the form of the above table, read the string, first converted to a number of 10, and then use the function for the operation to convert to other binary.

But at the same time, you can operate like this, read in and directly perform a forced type conversion operation to int type (int type data in pytdon is decimal)

Conversion from decimal to binary: bin(n)

n=int(input())
print(bin(n))

Input: 10

Output: 0b1010

Conversion from decimal to octal: oct(n)

n=int(input())
print(oct(n))

Input: 10

Output: 0o12

Convert decimal to hexadecimal: hex(n)

n=int(input())
print(hex(n))

Input: 10

Output: 0xa

Convert hexadecimal to other binary:

Convert hex to binary: bin(int(n,16))

n=input()
print(bin(int(n,16)))

Input: a

Output: 0b1010

Convert hexadecimal to octal: oct(int(n,16))

n=input()
print(oct(int(n,16)))

Input: a

Output: 0o12

Convert hexadecimal to decimal: int(n,16)

n=input()
print((int(n,16)))

Input: a

Output: 10

take note of: We can notice that, except for the conversion to decimal numbers, the results of the conversion to hexadecimal are prefixed with the prefix 0b for binary, 0o for octal, and 0x for hexadecimal.

But we don't need its prefix in the vast majority of our uses of the converted data.

So here the author introduces two ways to remove its prefix.

Methods for removing prefixes

Method 1: Use string slicing operation

principle: It is the result of the conversion of the conversion of the results of its string slicing operations, the results obtained from the third character to take the results can be, so we can get the results without prefixes

Examples:

//10 to binary conversion
n=int(input())
print(bin(n)[2:])//Slicing operations

Input: 10

Output: 1010

//10 to octal conversion
n=int(input())
print(oct(n)[2:])//Slicing operations

Input: 10

Output: 12

//Conversion from decimal to hexadecimal
n=int(input())
print(hex(n)[2:])//Slicing operations

Input: 10

Output: a

The author here to convert other decimal system for example, other cases can be analogous.

Method 2: Formatting numbers using the format function

clarification: First of all, I said earlier, the principle of conversion of different types of conversion to decimal first, and then use the relevant function to prohibit the conversion operation, while the format function inside the formatting of the number of methods can be directly achieved by the function of conversion, the following one by one:

Other types are converted to binary: manipulate: Adding a b to the slot of the format function will allow you to convert other types to binary, as shown in the following example.

n=input()
print("{:b}".format(int(n,8)))
// Converting an octal number to decimal first.
// Then add a b to the slot of format, which is equivalent to implementing the bin function
//But this result is without the0bprefixed

Input: 1010

Output: 1000001000

Other types are converted to octal:

manipulate: Add an o to the slot of the format function to realize the conversion of other types to hexadecimal.

n=input()
print("{:o}".format(int(n,16)))
// First convert a hexadecimal number to decimal.
//Then useformatFunction formatting numeric methods can be converted

Input: a

Output: 12

Other types are converted to hexadecimal: manipulate: Adding an x to the slot of the format function will allow you to convert other types to binary, as shown in the following example.

n=input()
print("{:x}".format(int(n,8)))
// First convert an octal number to hexadecimal.
//And then the same principle as above.

Input: 1010

Output: 208

Note: Since the conversion to decimal is supposed to be without a prefix, there is no need to control the formatting of numbers with the format method

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