TheAlgorithms/Go · error

input string is empty

Error message

input string is empty

What it means

Returned by hexToDecimal when the input string, after TrimSpace, has zero length; the conversion has no digits to parse, so the empty-input guard rejects it before prefix or format validation runs.

Source

Thrown at conversion/hexadecimaltodecimal.go:28

// Supported Hexadecimal number range is 0 to 7FFFFFFFFFFFFFFF.

package conversion

import (
	"fmt"
	"regexp"
	"strings"
)

var isValidHexadecimal = regexp.MustCompile("^[0-9A-Fa-f]+$").MatchString

// hexToDecimal converts a hexadecimal string to a decimal integer.
func hexToDecimal(hexStr string) (int64, error) {

	hexStr = strings.TrimSpace(hexStr)

	if len(hexStr) == 0 {
		return 0, fmt.Errorf("input string is empty")
	}

	// Check if the string has a valid hexadecimal prefix
	if len(hexStr) > 2 && (hexStr[:2] == "0x" || hexStr[:2] == "0X") {
		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' {

View on GitHub (pinned to 5ba447ec5f)

Solutions

  1. Check the string is non-empty (after TrimSpace) before converting
  2. Return 0 as a default for empty input when semantically acceptable
  3. Prompt the user for a value instead of passing empty strings
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at conversion/hexadecimaltodecimal.go:28 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of TheAlgorithms/Go@5ba447ec5f (2026-09-02). Data as JSON: /api/errors/ee15d3e7b458a2b6. Report an issue: GitHub.