On this page
binascii
Prev: bdb | Up: stdlib_module | Next: bisect
Rel: email, json, mailbox, mimetypes, base64
Ref: docs.python.org — binascii
Tags: #public #python3 #stdlib
Import
import binascii
Overview
Fast C-implemented binary/ASCII conversions (hexlify, unhexlify, crc32, b2a_base64, a2b_base64) and the binascii.Error exception shared by base64. For simple hex conversions, bytes.hex() and bytes.fromhex() are usually cleaner; reach for binascii for binascii.Error, crc32, or delimited hex formatting.
Quick Start
import binascii
raw = b"\xde\xad\xbe\xef"
hex_bytes = binascii.hexlify(raw, sep=":")
assert hex_bytes == b"de:ad:be:ef"
assert binascii.unhexlify(b"deadbeef") == raw
Implementation Walkthrough
1. Computing CRC32 Checksums & Catching Decoding Errors
import base64
import binascii
crc = binascii.crc32(b"payload") & 0xFFFFFFFF
assert isinstance(crc, int)
try:
base64.b64decode("!!!", validate=True)
except binascii.Error:
pass
Gotchas & Boundaries
- Prefer
raw.hex()/bytes.fromhex(s)when working with Pythonstrhex;binascii.hexlify()returnsbytes. - Use base64 for Base64/Base32/Base85:
binascii.b2a_base64()appends a trailing newline by default (newline=True).
Common Functions
- binascii.a2b_base64() —
a2b_base64(data, /, *, strict_mode=False)— Decode a line of base64 data. - binascii.a2b_hex() —
a2b_hex(hexstr, /)— Binary data of hexadecimal representation. - binascii.a2b_qp() —
a2b_qp(data, header=False)— Decode a string of qp-encoded data. - binascii.a2b_uu() —
a2b_uu(data, /)— Decode a line of uuencoded data. - binascii.b2a_base64() —
b2a_base64(data, /, *, newline=True)— Base64-code line of data. - binascii.b2a_hex() —
b2a_hex(...)— Hexadecimal representation of binary data. - binascii.b2a_qp() —
b2a_qp(data, quotetabs=False, istext=True, header=False)— Encode a string using quoted-printable encoding. - binascii.b2a_uu() —
b2a_uu(data, /, *, backtick=False)— Uuencode line of data. - binascii.crc32() —
crc32(data, crc=0, /)— Compute CRC-32 incrementally. - binascii.crc_hqx() —
crc_hqx(data, crc, /)— Compute CRC-CCITT incrementally. - binascii.hexlify() —
hexlify(...)— Hexadecimal representation of binary data. - binascii.unhexlify() —
unhexlify(hexstr, /)— Binary data of hexadecimal representation.
Classes & Objects
- binascii.Error —
binascii.Errorclass. (Methods:.add_note(),.with_traceback()) - binascii.Incomplete —
binascii.Incompleteclass. (Methods:.add_note(),.with_traceback())
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/base64
- tuts/python3/stdlib_module/bdb
- tuts/python3/stdlib_module/binascii/a2b_base64
- tuts/python3/stdlib_module/binascii/a2b_hex
- tuts/python3/stdlib_module/binascii/a2b_qp
- tuts/python3/stdlib_module/binascii/a2b_uu
- tuts/python3/stdlib_module/binascii/b2a_base64
- tuts/python3/stdlib_module/binascii/b2a_hex
- tuts/python3/stdlib_module/binascii/b2a_qp
- tuts/python3/stdlib_module/binascii/b2a_uu
- tuts/python3/stdlib_module/binascii/crc32
- tuts/python3/stdlib_module/binascii/crc_hqx
- tuts/python3/stdlib_module/binascii/Error
- tuts/python3/stdlib_module/binascii/hexlify
- tuts/python3/stdlib_module/binascii/Incomplete
- tuts/python3/stdlib_module/binascii/unhexlify
- tuts/python3/stdlib_module/bisect
- tuts/python3/stdlib_module/email
- tuts/python3/stdlib_module/json
- tuts/python3/stdlib_module/mailbox
- tuts/python3/stdlib_module/mimetypes
- tuts/python3/stdlib_module/stdlib_module
Related notes
- tuts/python3/stdlib_module/base64 Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/bdb Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/binascii/a2b_base64 Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/binascii/a2b_hex Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/binascii/a2b_qp Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/binascii/a2b_uu Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/binascii/b2a_base64 Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/binascii/b2a_hex Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/binascii/b2a_qp Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/binascii/b2a_uu Linked here; links to this page; shared tags: #stdlib