binascii – binary/ASCII conversions

This module implements conversions between binary data and various encodings of it in ASCII form (in both directions). It provides hex-string, base64, and CRC-32 helpers commonly used when transmitting binary data over text-only channels.

Functions

binascii.hexlify(data: bytes, sep: str | bytes | None = None) bytes

Convert the bytes in the data object to a hexadecimal representation. Returns a bytes object.

If the additional argument sep is supplied it is used as a separator between hexadecimal values.

binascii.unhexlify(data: str | bytes) bytes

Convert hexadecimal data to binary representation. Returns bytes string. (i.e. inverse of hexlify)

binascii.a2b_base64(data: str | bytes) bytes

Decode base64-encoded data, ignoring invalid characters in the input. Conforms to RFC 2045 s.6.8. Returns a bytes object.

binascii.b2a_base64(data: bytes, *, newline: bool = True) bytes

Encode binary data in base64 format, as in RFC 3548. Returns the encoded data followed by a newline character if newline is true, as a bytes object.

binascii.crc32(data: bytes, value: int = 0) int

Compute CRC-32, the 32-bit checksum of data, starting with an initial CRC of value. The default initial CRC is zero. The algorithm is consistent with the ZIP file checksum.