Python Glossary Русский

all Python 3binasciifunction

binascii.hexlify()

hex representation

Converts bytes to hexadecimal notation, also as bytes: b'\x01\xff' → b'01ff'. Since 3.8 it accepts a separator between groups.

Syntax

binascii.hexlify(data[, sep[, bytes_per_sep=1]])

The result is bytes, not str: printing shows b'01ff', and a string needs .decode(). That is why new code prefers data.hex(): the same job straight into str, separator included. hexlify lives on in old code and next to hashlib, whose digest() is already bytes. It does what b2a_hex does, as a separate object with identical behaviour.

Examples

import binascii
print(binascii.hexlify(b'\x01\xff'))  # → b'01ff'
print(binascii.hexlify(b'\x01\xff').decode())  # → 01ff
print(b'\x01\xff'.hex())  # → 01ff
import binascii
print(binascii.hexlify(b'\xb9\x01\xef', '-'))  # → b'b9-01-ef'

See also

Open in the full glossarydocs.python.orgReport an inaccuracy