nautechsystems/nautilus_trader · error

Invalid hex u32: {e}

Error message

Invalid hex u32: {e}

What it means

This error means a hex-encoded string failed to parse as a u32. Like parse_hex_u64, it strips the '0x' prefix and parses base 16; invalid hex characters, an empty string, or values above u32::MAX (more than 8 hex digits) cause the failure. Thrown by parse_hex_u32, typically via extract_transaction_index and extract_log_index.

Source

Thrown at crates/adapters/blockchain/src/rpc/log.rs:50

/// Parse hex string to u64.
///
/// # Errors
///
/// Returns an error if the hex string cannot be parsed as u64.
pub fn parse_hex_u64(hex: &str) -> anyhow::Result<u64> {
    u64::from_str_radix(hex.trim_start_matches("0x"), 16)
        .map_err(|e| anyhow::anyhow!("Invalid hex u64: {e}"))
}

/// Parse hex string to u32.
///
/// # Errors
///
/// Returns an error if the hex string cannot be parsed as u32.
pub fn parse_hex_u32(hex: &str) -> anyhow::Result<u32> {
    u32::from_str_radix(hex.trim_start_matches("0x"), 16)
        .map_err(|e| anyhow::anyhow!("Invalid hex u32: {e}"))
}

/// Extract block number from RPC log.
///
/// # Errors
///
/// Returns an error if the block number is missing or cannot be parsed.
pub fn extract_block_number(log: &RpcLog) -> anyhow::Result<u64> {
    let hex = log
        .block_number
        .as_ref()
        .ok_or_else(|| anyhow::anyhow!("Missing block number"))?;
    parse_hex_u64(hex)
}

/// Extract transaction hash from RPC log.
///
/// # Errors

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Verify the string is a valid hex quantity with at most 8 significant digits (0 to 0xffffffff).
  2. If the value may exceed u32 (e.g. some chains use wider indices), parse as u64 instead.
  3. Check you are not accidentally passing a hash or topic hex into this parser.
  4. Trim whitespace and validate with a regex before calling.

Example fix

// before
let idx = parse_hex_u32(some_topic_hex)?;
// after
let idx = u64::from_str_radix(some_topic_hex.trim_start_matches("0x"), 16)? as u32;
Defensive patterns

Strategy: validation

Validate before calling

fn is_hex_u32(s: &str) -> bool {
    let t = s.trim_start_matches("0x");
    !t.is_empty() && t.len() <= 8 && t.chars().all(|c| c.is_ascii_hexdigit())
}

Type guard

fn looks_like_hex_u32(s: &str) -> bool {
    matches!(u32::from_str_radix(s.trim_start_matches("0x"), 16), Ok(_))
}

Try / catch

match parse_hex_u32(hex) {
    Ok(v) => v,
    Err(e) => { log::warn!("bad hex u32 {hex}: {e}"); 0 }
}

Prevention

When it happens

Trigger: Calling parse_hex_u32 with "0x", empty strings, non-hex characters, or values above 0xffffffff (e.g. an over-long quantity hex from a malformed RPC payload).

Common situations: RPC nodes returning padded or malformed index fields, passing a 32-byte topic string (64 hex chars) into a u32 parser, or feeding hash strings into index parsers by mistake.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08). Data as JSON: /api/errors/8397019b3674a5d8. Report an issue: GitHub.