Bytearray Methods

bytearray.hex()

Render each byte as two lowercase hexadecimal digits, preserving leading zeroes. An optional single ASCII separator (str or bytes) groups the output. Positive bytes_per_sep counts groups from the right; negative counts from the left. A zero group size suppresses separators on the tested CPython runtime. This representation is not encryption or text decoding. The result is a separate string, and the receiver is not modified. To parse the output with fromhex(), omit the separator or choose ASCII whitespace; punctuation must be removed first.

Formal Syntax & Structure

python syntaxpython
data.hex()data.hex(sep, bytes_per_sep=1)

Parameters & Attributes

NameTypeDescription
sepstr | bytesOne ASCII separator character; omitted for contiguous hex.
bytes_per_sepintGroup width; sign selects alignment.

Return Value

str

Lowercase hexadecimal representation, optionally grouped.

Working Example

Example Snippetpython
packet = bytearray(b"ABC")label = packet.hex("-")packet[0] = 90print(label)print(packet.hex())print(type(label).__name__)
Python Reference