Python Glossary Русский

all Python 3base64function

base64.b16decode()

Base32 and Base16

Decodes Base16 (hex) into bytes. By default it accepts only capital letters A–F.

Syntax

base64.b16decode(s, casefold=False)

A lowercase hex string such as the output of bytes.hex() raises binascii.Error unless casefold=True is passed, a frequent surprise. Spaces are not allowed either. For arbitrary hex from a person or another program bytes.fromhex is simpler: it accepts both cases and spaces between bytes.

Examples

import base64
import binascii
try:
    base64.b16decode('cafe')
except binascii.Error as e:
    print(type(e).__name__)  # → Error
print(base64.b16decode('cafe', casefold=True), bytes.fromhex('ca fe'))  # → b'\xca\xfe' b'\xca\xfe'

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy