TheAlgorithms/Go · error
invalid character in hexadecimal string: %c
Error message
invalid character in hexadecimal string: %c
What it means
Defense-in-depth inside the accumulation loop: even after the regex check, a character that maps to no digit (theoretically unreachable for ASCII input, possible for multi-byte runes) aborts conversion naming the character.
Source
Thrown at conversion/hexadecimaltodecimal.go:51
hexStr = hexStr[2:]
}
// Validate the hexadecimal string
if !isValidHexadecimal(hexStr) {
return 0, fmt.Errorf("invalid hexadecimal string")
}
var decimalValue int64
for _, char := range hexStr {
var digit int64
if char >= '0' && char <= '9' {
digit = int64(char - '0')
} else if char >= 'A' && char <= 'F' {
digit = int64(char - 'A' + 10)
} else if char >= 'a' && char <= 'f' {
digit = int64(char - 'a' + 10)
} else {
return 0, fmt.Errorf("invalid character in hexadecimal string: %c", char)
}
decimalValue = decimalValue*16 + digit
}
return decimalValue, nil
}
View on GitHub (pinned to 5ba447ec5f)
Solutions
- Rely on the regex validation upstream; treat this as an invariant failure
- Report the exact offending character to aid debugging
- Log and skip rather than fail if streaming conversion is required
Defensive patterns
Strategy: type-guard
When it happens
Trigger: Thrown at conversion/hexadecimaltodecimal.go:51 when the library encounters an invalid state.
Common situations: See trigger scenarios.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
AI-assisted analysis of TheAlgorithms/Go@5ba447ec5f (2026-09-02).
Data as JSON: /api/errors/64ec2d82821bae58.
Report an issue: GitHub.