In this article, we will learn how to decode and encode hexadecimal numbers using Python.
Use binascii module
Use base64 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 thebinascii module.
Below are the algorithms/steps to perform the required task. −
Use the import keyword to import thebinasciimodule.
Create a variable to store theinput byte string.
Use theb2a_hex()function of thebinasciimodule to encode the input byte string into a hexadecimal number.
Print the resulting hexadecimal number of the input byte string.
Use thebinasciimodule'sa2b_hex()function to decode the above hexadecimal number into a byte string.
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))
When executing the above program, the following output will be generated -
b'7475746f7269616c73706f696e7420707974686f6e' b'tutorialspoint python'
base64The module also has similar functionality. It can also encode or decode raw hexadecimal numbers.
Below are the algorithms/steps to perform the required task. −
Use the import keyword to import thebase64module.
Create a variable to store theinput byte string.
Use theb16encode()function of thebase64module to encode the input byte string into hexadecimal digits (hexdigits).
Print the resulting hexadecimal number of the input byte string.
Use theb16decode()function of thebase64module to decode the above hexadecimal number into a byte string and print it.
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))
When executing the above program, the following output will be generated -
b'7475746f7269616c73706f696e7420707974686f6e' b'tutorialspoint python'
Converting to and from hexadecimal is simple in most cases using the functions described.Case foldingis the biggest difference between the two methods. In contrast to operations in binascii, thebase64.b16decode()andbase64.b16encode()functions can only handleuppercasehexadecimal 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 ofThe 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'))
When executing the above program, the following output will be generated -
b'7475746F7269616C73706F696E7420707974686F6E' 7475746F7269616C73706F696E7420707974686F6E
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 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!