all Python 3binasciifunction
binascii.b2a_hex()
hex representation
Converts bytes to hexadecimal notation as bytes, the same as hexlify. Since 3.8 it can insert a separator every bytes_per_sep bytes.
Syntax
binascii.b2a_hex(data[, sep[, bytes_per_sep=1]])
The separator has a subtlety: groups are counted from the RIGHT end, so with bytes_per_sep=2 and an odd length the short group ends up on the left; a negative value counts from the left. The separator is a single character, and a two-character string raises ValueError. The name reads as 'binary to ASCII', the pattern of the whole module: b2a_* encodes, a2b_* decodes.
Examples
import binascii
data = b'\xb9\x01\xef'
print(binascii.b2a_hex(data, b'_', 2)) # → b'b9_01ef'
print(binascii.b2a_hex(data, b' ', -2)) # → b'b901 ef'
try:
binascii.b2a_hex(data, '::')
except ValueError:
print('one char only') # → one char only
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy