hex_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) -> bytesParameters
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 resultNotes
Equivalent to
bytes.fromhex(hex_string.removeprefix("0x"))Does not validate length — use
Bytes32orPrivateKeyfor length-checked valuesFor loading private keys from hex, prefer
PrivateKey.from_hex_str()which combines decoding and validation
See Also
PrivateKey.from_hex_str() — hex string to private key
Bytes32 — 32-byte value type
Last updated

