{"record":{"id":"505e7edbb2974785","repo":"TheAlgorithms/Go","slug":"input-string-is-empty","errorCode":null,"errorMessage":"input string is empty","messagePattern":"input string is empty","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"conversion/hexadecimaltobinary.go","lineNumber":30,"sourceCode":"package conversion\n\nimport (\n\t\"errors\"\n\t\"regexp\"\n\t\"strings\"\n)\n\nvar isValidHex = regexp.MustCompile(\"^[0-9A-Fa-f]+$\").MatchString\n\n// hexToBinary() function that will take Hexadecimal number as string,\n// and return its Binary equivalent as a string.\nfunc hexToBinary(hex string) (string, error) {\n\t// Trim any leading or trailing whitespace\n\thex = strings.TrimSpace(hex)\n\n\t// Check if the hexadecimal string is empty\n\tif hex == \"\" {\n\t\treturn \"\", errors.New(\"input string is empty\")\n\t}\n\n\t// Check if the hexadecimal string is valid\n\tif !isValidHex(hex) {\n\t\treturn \"\", errors.New(\"invalid hexadecimal string: \" + hex)\n\t}\n\n\t// Parse the hexadecimal string to an integer\n\tvar decimal int64\n\tfor i := 0; i < len(hex); i++ {\n\t\tchar := hex[i]\n\t\tvar value int64\n\t\tif char >= '0' && char <= '9' {\n\t\t\tvalue = int64(char - '0')\n\t\t} else if char >= 'A' && char <= 'F' {\n\t\t\tvalue = int64(char - 'A' + 10)\n\t\t} else if char >= 'a' && char <= 'f' {\n\t\t\tvalue = int64(char - 'a' + 10)","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/conversion/hexadecimaltobinary.go#L12-L48","documentation":"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.","triggerScenarios":"Calling the hex-to-binary conversion with \"\", a string of only spaces/tabs (TrimSpace reduces it to \"\"), or an unset variable.","commonSituations":"Empty environment variable or config field holding the hex value; reading a zero-byte file; splitting input where an empty tail element is produced.","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."],"exampleFix":"// before\nout, err := conversion.HexToBinary(hexInput) // empty input -> error\n// after\nif strings.TrimSpace(hexInput) == \"\" { return errors.New(\"hex input required\") }\nout, err = conversion.HexToBinary(hexInput)","handlingStrategy":"validation","validationCode":"func hasHexInput(hex string) bool { return strings.TrimSpace(hex) != \"\" }","typeGuard":null,"tryCatchPattern":"out, err := conversion.HexToBinary(hex)\nif err != nil {\n    if err.Error() == \"input string is empty\" {\n        return fmt.Errorf(\"hex input required\")\n    }\n    return err\n}","preventionTips":["Check TrimSpace non-empty before converting.","Default empty config/env values explicitly.","Guard file reads against zero-byte content."],"tags":["conversion","hexadecimal","go","empty-input"],"backgroundTag":"empty-input-rejected","analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}