TheAlgorithms/Go · error
invalid hexadecimal string:
Error message
invalid hexadecimal string:
What it means
hexToBinary validates the string with isValidHex and returns this error (with the offending input appended) when the string is not valid hexadecimal. The dynamic message embeds the rejected value to ease debugging. It precedes the per-character parse loop.
Source
Thrown at conversion/hexadecimaltobinary.go:35
"strings"
)
var isValidHex = regexp.MustCompile("^[0-9A-Fa-f]+$").MatchString
// hexToBinary() function that will take Hexadecimal number as string,
// and return its Binary equivalent as a string.
func hexToBinary(hex string) (string, error) {
// Trim any leading or trailing whitespace
hex = strings.TrimSpace(hex)
// Check if the hexadecimal string is empty
if hex == "" {
return "", errors.New("input string is empty")
}
// Check if the hexadecimal string is valid
if !isValidHex(hex) {
return "", errors.New("invalid hexadecimal string: " + hex)
}
// Parse the hexadecimal string to an integer
var decimal int64
for i := 0; i < len(hex); i++ {
char := hex[i]
var value int64
if char >= '0' && char <= '9' {
value = int64(char - '0')
} else if char >= 'A' && char <= 'F' {
value = int64(char - 'A' + 10)
} else if char >= 'a' && char <= 'f' {
value = int64(char - 'a' + 10)
} else {
return "", errors.New("invalid character in hexadecimal string: " + string(char))
}
decimal = decimal*16 + value
}View on GitHub (pinned to 5ba447ec5f)
Solutions
- Strip a leading "0x"/"0X" prefix and remove whitespace/separators before calling.
- Pre-validate with a regexp ^[0-9a-fA-F]+$ and reject early.
- Parse the error message to identify the offending string and fix the input source.
Example fix
// before
out, err := conversion.HexToBinary("0x1A3F") // invalid hexadecimal string: 0x1A3F
// after
clean := strings.TrimPrefix(strings.TrimPrefix(raw, "0x"), "0X")
out, err = conversion.HexToBinary(clean) Defensive patterns
Strategy: validation
Validate before calling
var hexRe = regexp.MustCompile(`^[0-9a-fA-F]+$`)
func isHex(s string) bool {
s = strings.TrimPrefix(strings.TrimPrefix(s, "0x"), "0X")
return hexRe.MatchString(s)
} Try / catch
out, err := conversion.HexToBinary(hex)
if err != nil {
if strings.HasPrefix(err.Error(), "invalid hexadecimal string:") {
return fmt.Errorf("bad hex input: %w", err)
}
return err
} Prevention
- Strip 0x/0X prefixes from dump output.
- Remove whitespace and separators before parsing.
- Validate with ^[0-9a-fA-F]+$ first.
When it happens
Trigger: Calling the hex conversion with strings like "0x1A" (prefix not stripped), "GG", "12 34", or hex with a sign or decimal point.
Common situations: Copy-pasting hex values that include the "0x"/"0X" prefix, whitespace, or byte separators (":" or "-") from dump tools.
Related errors
- invalid character in hexadecimal string:
- not a valid binary string
- input string is empty
- binary number must be in range 0 to 2^(31-1)
- integer must have +ve value
AI-assisted analysis of TheAlgorithms/Go@5ba447ec5f (2026-09-02).
Data as JSON: /api/errors/a5cc64eb6247f474.
Report an issue: GitHub.