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 ¶
- func DecodeString(value string) ([]byte, error)
- func DecodeStringOrDefault(value string, defaultValue []byte) []byte
- func EncodeToString(data []byte) string
- func EncodeWithPrefix(data []byte) string
- func IsHexString(value string) bool
- func IsValid(value string) bool
- func MustDecodeString(value string) []byte
- func Normalize(value string) string
- func TrimPrefix(value string) string
- func TryDecode(value string) ([]byte, bool)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DecodeString ¶
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 ¶
DecodeStringOrDefault decodes a hex string, returning the default value on error.
func EncodeToString ¶
EncodeToString converts bytes to a hex string without "0x" prefix. This is the inverse of DecodeString/TrimPrefix.
func EncodeWithPrefix ¶
EncodeWithPrefix converts bytes to a hex string with "0x" prefix. Useful for displaying hex values to users.
func IsHexString ¶
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 ¶
IsValid checks if a string is a valid hex string. It handles optional "0x" or "0X" prefix.
func MustDecodeString ¶
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 ¶
Normalize returns a normalized hex string (lowercase, no 0x prefix). Useful for comparing hex addresses or storing in a canonical format.
func TrimPrefix ¶
TrimPrefix removes "0x" or "0X" prefix from hex strings if present. This is the standard way to strip prefixes before hex operations.
Types ¶
This section is empty.