Home >Backend Development >Python Tutorial >How to perform hexadecimal conversion in python

How to perform hexadecimal conversion in python

angryTom
angryTomOriginal
2020-03-02 17:39:5316016browse

How to perform hexadecimal conversion in python

How to perform base conversion in python

1. Convert decimal to binary (bin)

First, let’s look at how to convert a decimal into binary. We can use python’s built-in method bin

dec=10
print bin(dec)

Output

0b1010

The binary in python starts with ob

Recommendation: " python video tutorial

2. Convert decimal to octal (oct)

Let’s take a look at converting decimal to octal using the method oct(dec )

dec=10
print oct(dec)

Output

012

3. Convert decimal to hexadecimal (hex)

Then convert decimal to hexadecimal, also using python’s built-in The method performs hex(dec)

dec=10
print hex(dec)

output hexadecimal

0xa

4, binary conversion to decimal

dec=10
 print str(int(bin(dec), 2))

output

10

5 , Convert octal to binary

Similar to the method of converting octal to binary

dec=10
print str(int(oct(dec), 8))

Output

10

6. Convert 16 to 10

Finally let’s look at the conversion of hexadecimal into decimal

dec=10
print str(int(hex(dec).upper(), 16))

For more Python tutorials, please pay attention to PHP Chinese website!

The above is the detailed content of How to perform hexadecimal conversion in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn