Python binascii Module
Example
Convert bytes to hex and back:
import binascii
s = b"Emil"
h = binascii.hexlify(s)
print(h)
print(binascii.unhexlify(h).decode())
Try it Yourself »
Definition and Usage
The binascii module converts between binary and various ASCII representations (like hex and Base64 helpers).
Use it for fast conversions at C speed when dealing with checksums, binary payloads, or low-level encodings.
Members
| Member | Description |
|---|---|
| a2b_base64() | Decode Base64 data to binary. |
| a2b_hex() | Decode hexadecimal string to binary data. |
| a2b_hqx() | Decode binhex4 formatted data. |
| a2b_qp() | Decode quoted-printable data to binary. |
| a2b_uu() | Decode uuencoded data to binary. |
| b2a_base64() | Encode binary data to Base64 (returns bytes with trailing newline). |
| b2a_hex() | Encode binary data to hexadecimal representation. |
| b2a_hqx() | Encode binary data using binhex4 format (RLE-compressed). |
| b2a_qp() | Encode binary data using quoted-printable. |
| b2a_uu() | Encode binary data using uuencoding. |
| crc32() | Compute CRC-32 checksum of data. |
| crc_hqx() | Compute CRC-16-CCITT of data with initial value. |
| Error | Exception raised for decoding/encoding errors. |
| hexlify() | Return the hexadecimal representation of binary data. |
| Incomplete | Exception raised when input data ends unexpectedly. |
| rledecode_hqx() | Decode RLE-compressed binhex4 data. |
| rlecode_hqx() | Encode binary data with RLE compression for binhex4 format. |
| unhexlify() | Convert a hexadecimal string to binary data. |