Decoding and encoding hexadecimal numbers using Python

PHPz
Release: 2023-08-19 14:09:22
forward
2445 people have browsed it

Decoding and encoding hexadecimal numbers using Python

In this article, we will learn how to decode and encode hexadecimal numbers using Python.

usage instructions

  • Use binascii module

  • Use base64 module

Method 1: Using Binascii module

In the binascii module, there are several methods to convert between binary and different ASCII-encoded binary representations.

If you only need to encode or decode a raw hexadecimal digit string, you can use the binascii module.

Algorithm (steps)

Below are the algorithms/steps to perform the required task. −

  • Use the import keyword to import the binascii module.

  • Create a variable to store the input byte string.

  • Use the b2a_hex() function of the binascii module to encode the input byte string into a hexadecimal number.

  • Print the resulting hexadecimal number of the input byte string.

  • Use the binascii module's a2b_hex() function to decode the above hexadecimal number into a byte string.

The Chinese translation of

Example

is:

Example

The following program uses the b2a_hex() and a2b_hex() functions to encode an input byte string into a hexadecimal number and decode it back to a byte string.

# importing binascii module
import binascii

# input byte string 
inputByteString = b'tutorialspoint python'

# encoding input byte string into hexadecimal digits
hexdigits = binascii.b2a_hex(inputByteString)

# printing the resultant hexadecimal digits of the byte string 
print(hexdigits)

# decoding hexadecimal digits back into byte string 
print(binascii.a2b_hex(hexdigits))
Copy after login

Output

When executing the above program, the following output will be generated -

b'7475746f7269616c73706f696e7420707974686f6e'
b'tutorialspoint python'
Copy after login
Copy after login

Method 2: Use base64 module

base64 The module also has similar functionality. It can also encode or decode raw hexadecimal numbers.

Algorithm (steps)

Below are the algorithms/steps to perform the required task. −

  • Use the import keyword to import the base64 module.

  • Create a variable to store the input byte string.

  • Use the b16encode() function of the base64 module to encode the input byte string into hexadecimal digits (hexdigits).

  • Print the resulting hexadecimal number of the input byte string.

  • Use the b16decode() function of the base64 module to decode the above hexadecimal number into a byte string and print it.

The Chinese translation of

Example

is:

Example

The following program encodes an input byte string into a hexadecimal number and decodes it back to a byte string using the b16encode() and b16decode() functions -

# importing base64 module
import base64

# input byte string 
inputByteString = b'tutorialspoint python'

# encoding input byte byte string into hexadecimal digits
# using b16encode() function of base64 module
hexdigits = base64.b16encode(inputByteString)

# printing the resultant hexadecimal digits of the byte string 
print(hexdigits)

# decoding hexadecimal digits back into byte string 
print(base64.b16decode(hexdigits))
Copy after login

Output

When executing the above program, the following output will be generated -

b'7475746f7269616c73706f696e7420707974686f6e'
b'tutorialspoint python'
Copy after login
Copy after login

Converting to and from hexadecimal is simple in most cases using the functions described. Case folding is the biggest difference between the two methods. In contrast to operations in binascii, the base64.b16decode() and base64.b16encode() functions can only handle uppercase hexadecimal letters.

It is also important to remember that the output of an encoding function is always a byte string. You may need to include additional decoding steps to force output to Unicode.

The Chinese translation of

Example

is:

Example

The following program uses the decode function to decode hexadecimal numbers into ASCII format:

# importing base64 module
import base64
 
# input byte string 
inputByteString = b'tutorialspoint python'
 
# encoding input byte byte string into hexadecimal digits
# using b16encode() function of base64 module
hexdigits = base64.b16encode(inputByteString)
 
# printing the resultant hexadecimal digitsof the byte string 
print(hexdigits)
 
# decoding hexadecimal digits in ASCII format using the decode() function
print(hexdigits.decode('ascii'))
Copy after login

Output

When executing the above program, the following output will be generated -

b'7475746F7269616C73706F696E7420707974686F6E'
7475746F7269616C73706F696E7420707974686F6E
Copy after login

The b16decode() and a2b_hex() methods accept bytes or Unicode text as input when decoding hexadecimal numbers. However, these strings can only contain hexadecimal digits that have been ASCII encoded.

in conclusion

In this article, we learned how to use Python to decode and encode the hexadecimal representation of a number. We also learned how to use the decode() method to decode the ASCII representation of a hexadecimal number.

The above is the detailed content of Decoding and encoding hexadecimal numbers using Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!