TheAlgorithms/Go · error
input string is empty
Error message
input string is empty
What it means
The unexported hexToBinary helper in conversion/hexadecimaltobinary.go trims whitespace and rejects an empty string with this error before parsing. It guarantees the parser loop never runs on zero-length input. It surfaces through the package's exported wrapper when the user passes a blank string.
Source
Thrown at conversion/hexadecimaltobinary.go:30
package conversion
import (
"errors"
"regexp"
"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)View on GitHub (pinned to 5ba447ec5f)
Solutions
- Check strings.TrimSpace(hex) != "" before calling.
- Provide a default value when the input source is empty.
- Handle the returned error and show a 'hex input required' validation message.
Example fix
// before
out, err := conversion.HexToBinary(hexInput) // empty input -> error
// after
if strings.TrimSpace(hexInput) == "" { return errors.New("hex input required") }
out, err = conversion.HexToBinary(hexInput) Defensive patterns
Strategy: validation
Validate before calling
func hasHexInput(hex string) bool { return strings.TrimSpace(hex) != "" } Try / catch
out, err := conversion.HexToBinary(hex)
if err != nil {
if err.Error() == "input string is empty" {
return fmt.Errorf("hex input required")
}
return err
} Prevention
- Check TrimSpace non-empty before converting.
- Default empty config/env values explicitly.
- Guard file reads against zero-byte content.
When it happens
Trigger: Calling the hex-to-binary conversion with "", a string of only spaces/tabs (TrimSpace reduces it to ""), or an unset variable.
Common situations: Empty environment variable or config field holding the hex value; reading a zero-byte file; splitting input where an empty tail element is produced.
Related errors
- invalid hexadecimal string:
- invalid character in hexadecimal string:
- no text to encrypt
- not a valid binary string
- binary number must be in range 0 to 2^(31-1)
AI-assisted analysis of TheAlgorithms/Go@5ba447ec5f (2026-09-02).
Data as JSON: /api/errors/505e7edbb2974785.
Report an issue: GitHub.