How to perform hexadecimal conversion in python

angryTom
Release: 2020-03-02 17:39:53
Original
15962 people have browsed it

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)
Copy after login

Output

0b1010
Copy after login

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)
Copy after login

Output

012
Copy after login

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)
Copy after login

output hexadecimal

0xa
Copy after login

4, binary conversion to decimal

dec=10
 print str(int(bin(dec), 2))
Copy after login

output

10
Copy after login

5 , Convert octal to binary

Similar to the method of converting octal to binary

dec=10
print str(int(oct(dec), 8))
Copy after login

Output

10
Copy after login

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))
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template