Bytes Methods
bytes.fromhex()
Parse textual hexadecimal pairs into immutable bytes. 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. This class method constructs binary data rather than interpreting those bytes as Unicode text.
Formal Syntax & Structure
python syntaxpython
bytes.fromhex(string, /)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| string | str | ASCII hexadecimal digit pairs, optionally separated by ASCII whitespace. |
Return Value
bytes
Parsed binary sequence.
Working Example
Example Snippetpython
packet = bytes.fromhex("00 41 fF\n42")print(list(packet))print(packet.hex())print(type(packet).__name__)