Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Decoding and encoding hexadecimal digitals using Python
In this article, we will learn how to decode and encode hexadecimal digits using Python. Python provides built-in modules like binascii and base64 that make hexadecimal conversion straightforward.
What are Hexadecimal Digits?
Hexadecimal is a base-16 number system that uses digits 0-9 and letters A-F to represent values. It's commonly used in programming to represent binary data in a more readable format.
Method 1: Using binascii Module
The binascii module provides functions to convert between binary and different ASCII-encoded binary representations. It's ideal when you need to encode or decode raw hex digit strings.
Encoding and Decoding with binascii
The following example demonstrates encoding a byte string to hexadecimal and decoding it back ?
import binascii
# Input byte string
input_data = b'tutorialspoint python'
# Encoding byte string to hexadecimal digits
hex_encoded = binascii.b2a_hex(input_data)
print("Encoded:", hex_encoded)
# Decoding hexadecimal digits back to byte string
decoded_data = binascii.a2b_hex(hex_encoded)
print("Decoded:", decoded_data)
Encoded: b'7475746f7269616c73706f696e7420707974686f6e' Decoded: b'tutorialspoint python'
Method 2: Using base64 Module
The base64 module also provides hexadecimal encoding and decoding capabilities through its b16encode() and b16decode() functions.
Basic Encoding and Decoding
Here's how to use base64 for hexadecimal conversion ?
import base64
# Input byte string
input_data = b'tutorialspoint python'
# Encoding to hexadecimal using base64
hex_encoded = base64.b16encode(input_data)
print("Encoded:", hex_encoded)
# Decoding back to byte string
decoded_data = base64.b16decode(hex_encoded)
print("Decoded:", decoded_data)
Encoded: b'7475746F7269616C73706F696E7420707974686F6E' Decoded: b'tutorialspoint python'
Converting to ASCII Format
To get a string representation instead of bytes, use the decode() method ?
import base64
# Input byte string
input_data = b'tutorialspoint python'
# Encoding to hexadecimal
hex_encoded = base64.b16encode(input_data)
print("Bytes format:", hex_encoded)
# Converting to ASCII string format
hex_string = hex_encoded.decode('ascii')
print("ASCII format:", hex_string)
Bytes format: b'7475746F7269616C73706F696E7420707974686F6E' ASCII format: 7475746F7269616C73706F696E7420707974686F6E
Key Differences Between Methods
| Feature | binascii | base64 |
|---|---|---|
| Case sensitivity | Lowercase output | Uppercase output |
| Functions | b2a_hex(), a2b_hex() | b16encode(), b16decode() |
| Input flexibility | More flexible | Stricter case requirements |
Important Notes
Case sensitivity: The base64 module functions work only with uppercase hexadecimal letters, while binascii is more flexible with case handling.
Output format: Both methods return byte strings by default. Use decode('ascii') to convert to regular strings when needed.
Conclusion
Both binascii and base64 modules provide effective ways to encode and decode hexadecimal data in Python. Choose binascii for flexibility or base64 when working with uppercase hex formats.
