Bytearray Methods
bytearray.fromhex()
Parse textual hexadecimal pairs into a new mutable bytearray. Each complete pair represents one value from 0 to 255. Uppercase and lowercase ASCII hex digits are accepted; ASCII whitespace between byte pairs is ignored, including tabs and line breaks. Whitespace inside a pair, odd digit counts, punctuation, 0x prefixes and non-ASCII whitespace are invalid. In Python 3.13 the input must be str, not encoded bytes. Use hex() for the reverse conversion. Modify the returned buffer using integer indexing or slicing.
Formal Syntax & Structure
python syntaxpython
bytearray.fromhex(string, /)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| string | str | ASCII hexadecimal digit pairs, optionally separated by ASCII whitespace. |
Return Value
bytearray
Parsed binary sequence.
Working Example
Example Snippetpython
packet = bytearray.fromhex("01 02 ff")packet[1] = 16print(list(packet))print(packet.hex())print(type(packet).__name__)