viem-goviem-go

isHex

Checks if a string is a valid hex string

isHex

Checks if a string is a valid hex string.

Import

import "github.com/ChefBingbong/viem-go/utils/data"

Usage

import "github.com/ChefBingbong/viem-go/utils/data"
// Valid hex string
valid := data.IsHex("0x0102")
// true
// Valid empty hex
valid = data.IsHex("0x")
// true
// Invalid (no prefix)
valid = data.IsHex("0102")
// false
// Invalid characters (strict mode)
valid = data.IsHex("0xgg", data.IsHexOptions{Strict: true})
// false
// Valid prefix only (non-strict mode)
valid = data.IsHex("0xgg", data.IsHexOptions{Strict: false})
// true

Returns

  • Type: bool

true if the string is a valid hex string, false otherwise.

Parameters

value (required)

  • Type: string

The string to validate.

valid := data.IsHex("0x0102")

opts.strict (optional)

  • Type: bool
  • Default: true

If true, validates that the string contains only valid hex characters (0-9, a-f, A-F). If false, only checks for the 0x prefix.

// Strict mode (default) - validates hex characters
valid := data.IsHex("0x0102", data.IsHexOptions{Strict: true})
// true
valid = data.IsHex("0xgg", data.IsHexOptions{Strict: true})
// false
// Non-strict mode - only checks prefix
valid = data.IsHex("0xgg", data.IsHexOptions{Strict: false})
// true

Convenience Function

IsHexString

Alias for IsHex with Strict: true:

valid := data.IsHexString("0x0102")
// true