arrows-rotatehex_to_bytes

Convert a hex string to raw bytes

Convert a hex string to raw bytes, stripping an optional 0x prefix.

Signature

def hex_to_bytes(hex_string: str) -> bytes

Parameters

Parameter
Type
Required
Description

hex_string

str

Yes

Hex-encoded string, with or without "0x" prefix

Returns

Type
Description

bytes

Decoded raw bytes

Raises ValueError if hex_string contains non-hex characters or has odd length.

Example

from seismic_web3 import hex_to_bytes

data = hex_to_bytes("0xdeadbeef")  # b'\xde\xad\xbe\xef'
data = hex_to_bytes("deadbeef")    # same result

Notes

  • Equivalent to bytes.fromhex(hex_string.removeprefix("0x"))

  • Does not validate length — use Bytes32 or PrivateKey for length-checked values

  • For loading private keys from hex, prefer PrivateKey.from_hex_str() which combines decoding and validation

See Also

Last updated