hex

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 28, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package hex provides unified hexadecimal string handling utilities. This eliminates duplication across the codebase where hex encoding/decoding with 0x prefix handling is repeated.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeString

func DecodeString(value string) ([]byte, error)

DecodeString decodes a hex string to bytes. It handles optional "0x" or "0X" prefix automatically. Returns an error if the string contains invalid hex characters.

func DecodeStringOrDefault

func DecodeStringOrDefault(value string, defaultValue []byte) []byte

DecodeStringOrDefault decodes a hex string, returning the default value on error.

func EncodeToString

func EncodeToString(data []byte) string

EncodeToString converts bytes to a hex string without "0x" prefix. This is the inverse of DecodeString/TrimPrefix.

func EncodeWithPrefix

func EncodeWithPrefix(data []byte) string

EncodeWithPrefix converts bytes to a hex string with "0x" prefix. Useful for displaying hex values to users.

func IsHexString

func IsHexString(value string) bool

IsHexString checks if a string looks like hex encoded data. It does a quick character check without full decoding. Returns false for empty strings or odd-length strings.

func IsValid

func IsValid(value string) bool

IsValid checks if a string is a valid hex string. It handles optional "0x" or "0X" prefix.

func MustDecodeString

func MustDecodeString(value string) []byte

MustDecodeString decodes a hex string to bytes, panicking on error. Use this only for constants or values known to be valid at compile time.

func Normalize

func Normalize(value string) string

Normalize returns a normalized hex string (lowercase, no 0x prefix). Useful for comparing hex addresses or storing in a canonical format.

func TrimPrefix

func TrimPrefix(value string) string

TrimPrefix removes "0x" or "0X" prefix from hex strings if present. This is the standard way to strip prefixes before hex operations.

func TryDecode

func TryDecode(value string) ([]byte, bool)

TryDecode decodes a hex string, returning (data, true) on success or (nil, false) on any error. This matches the pattern used in marble.go for secret validation.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL