Python bytes.fromhex() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
classmethod

Definition

bytes.fromhex(string) reads a str of hexadecimal digits, two per byte, and returns the matching bytes object. Upper- and lowercase digits are both accepted.

ASCII whitespace between bytes is ignored, so "de ad be ef" works (since Python 3.7 all ASCII whitespace, including newlines and tabs, is skipped). It is the inverse of hex().

bytes.fromhex() is a classmethod: call it on the class, as bytes.fromhex("..."). bytearray.fromhex() works the same way and returns a bytearray.

Syntax

Call it on the class and pass a hex string.

python
data = bytes.fromhex(hex_string)
CallWhat it does
bytes.fromhex("4869")Returns b'Hi'.
bytes.fromhex("DE AD")Returns b'\xde\xad'.
bytes.fromhex("abc")Raises ValueError: odd number of digits.

Whitespace may separate bytes but cannot split the two digits of one byte.

Method signatures
python
classmethod bytes.fromhex(string)
# string: str of hex digits; ASCII whitespace between bytes is ignored

Parameters

ParameterDescription
stringA str containing pairs of hex digits, optionally separated by ASCII whitespace.

Return Value

Return ValueDescription
bytesA new bytes object with one byte per pair of digits.

Python Examples

Example 1 — Turn Hex Strings into bytes

Parse a compact hex string, a spaced dump, and uppercase digits that include printable and control bytes.

python
print(bytes.fromhex("48656c6c6f"))
print(bytes.fromhex("de ad be ef"))
print(bytes.fromhex("7E 00 0A"))

How It Works

  1. ASCII text. 48 65 6c 6c 6f are the codes for Hello, so the repr shows letters.
  2. Spaces allowed. Whitespace between bytes is skipped; the non-ASCII values print as escapes.
  3. Mixed repr. 0x7E is the printable ~, 0x00 prints as \x00, and 0x0A prints as \n.

Example 2 — Valid and Invalid Hex Strings

Try four inputs and catch ValueError for the bad ones. Newlines and tabs between bytes are fine on Python 3.7+.

python
for text in ["4142", "414", "41 4G", "41\n42\t43"]:
    try:
        print(bytes.fromhex(text))
    except ValueError:
        print("invalid hex:", repr(text))

How It Works

  1. Two bytes. "4142" is A and B.
  2. Odd length. Three digits cannot form whole bytes, so ValueError is raised.
  3. Bad digit. G is not a hex digit.
  4. Any ASCII whitespace. The newline and tab between pairs are ignored, giving three bytes.

Example 3 — Round Trip with hex() and bytearray.fromhex()

Convert bytes to hex text and back, then build a mutable bytearray from the same text.

python
original = b"\x00\xffPy"

text = original.hex()
print(text)
print(bytes.fromhex(text) == original)
print(bytearray.fromhex(text))

How It Works

  1. To text. hex() writes each of the four bytes as two digits.
  2. Lossless. fromhex() rebuilds exactly the original bytes.
  3. Mutable copy. bytearray.fromhex() returns a bytearray with the same values.

Frequently Asked Questions

Use bytes.fromhex("48656c6c6f"), which returns b'Hello'.
Yes. ASCII whitespace between bytes is ignored. Since Python 3.7 that includes newlines and tabs.
The 0x prefix is not allowed. Remove it first: bytes.fromhex(s.removeprefix("0x")) on Python 3.9+, or s[2:].
Remove the colons first: bytes.fromhex(mac.replace(":", "")).
It is a classmethod of bytes: it creates a new bytes object from the text you pass.

Next: bytes.hex()

Return a str of two hex digits per byte, with an optional separator.

hex() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful