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 stringvalid := data.IsHex("0x0102")// true
// Valid empty hexvalid = 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})// trueReturns
- 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 charactersvalid := data.IsHex("0x0102", data.IsHexOptions{Strict: true})// true
valid = data.IsHex("0xgg", data.IsHexOptions{Strict: true})// false
// Non-strict mode - only checks prefixvalid = data.IsHex("0xgg", data.IsHexOptions{Strict: false})// trueConvenience Function
IsHexString
Alias for IsHex with Strict: true:
valid := data.IsHexString("0x0102")
// true