Decoding and encoding hexadecimal digitals using Python


In this article, we will learn how to decode and encode hexadecimal digits using python.

Methods Used

  • Using binascii module

  • Using the base64 module

Method 1: Using Binascii Module

There are several methods to convert between binary and different ASCII-encoded binary representations in the binascii module.

Use the binascii module if all you need to do is encode or decode a raw string of hex digits.

Algorithm (Steps)

Following are the Algorithms/steps to be followed to perform the desired 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 binascii module to encode the input byte string into hexadecimal digits.

  • Print the resultant hexadecimal digits of the input byte string.

  • Use the a2b_hex() function of binascii module to decode the above hexadecimal digits back into a byte string.

Example

The following program encodes the input byte string into hexadecimal digits and decodes it back to a byte string using b2a_hex(), and a2b_hex() functions −

# 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))

Output

On execution, the above program will generate the following output −

b'7475746f7269616c73706f696e7420707974686f6e'
b'tutorialspoint python'

Method 2: Using the base64 Module

The base64 module also has similar functionalities as well. It can also encode or decode a raw string of hex digits.

Algorithm (Steps)

Following are the Algorithms/steps to be followed to perform the desired 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 hexdigits(hexadecimal digits).

  • Print the resultant hexadecimal digits of the input byte string.

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

Example

The following program encodes the input byte string into hexadecimal digits and decodes it back to a byte string using 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))

Output

On execution, the above program will generate the following output −

b'7475746F7269616C73706F696E7420707974686F6E'
b'tutorialspoint python'

Using the functions described, converting to and from hex is, for the most part, simple. Case folding is where the two methods differ most. In contrast to the operations in binascii, the base64.b16decode() and base64.b16encode() functions only work with uppercase hexadecimal letters.

It's also crucial to remember that the encoding functions' output is always a byte string. You might need to include an additional decoding step to force it to output in Unicode.

Example

The following program decodes the hexadecimal digits back as the ASCII format using the decode function −

# 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'))

Output

On execution, the above program will generate the following output −

b'7475746F7269616C73706F696E7420707974686F6E'
7475746F7269616C73706F696E7420707974686F6E

The b16decode() and a2b_hex() methods accept either bytes or Unicode texts as input when decoding hex digits. However, those strings can only have hexadecimal digits that have been ASCII-encoded.

Conclusion

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

Updated on: 23-Jan-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements