Python Glossary Русский

all Python 3Byte sequencesfunction

bytearray.fromhex

bytearray — encoding

A class method that creates a bytearray from a hex string.

Syntax

ba.fromhex(...)

It is called on the type itself — bytearray.fromhex(...) — no existing object needed. ASCII whitespace inside the string is skipped, but an odd number of digits or any non-hex character raises ValueError; it is the exact inverse of .hex().

Examples

print(bytearray.fromhex('616263'))   # → bytearray(b'abc')
print(bytearray.fromhex('61 62 63'))   # → bytearray(b'abc')
print(bytearray.fromhex('48656c6c6f').decode())   # → Hello
print(list(bytearray.fromhex('ff00')))   # → [255, 0]
print(bytearray.fromhex('6'))   # → ValueError

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy