{"record":{"id":"b913f2eae5747c83","repo":"TheAlgorithms/Go","slug":"invalid-character-in-hexadecimal-string","errorCode":null,"errorMessage":"invalid character in hexadecimal string: ","messagePattern":"invalid character in hexadecimal string: ","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"conversion/hexadecimaltobinary.go","lineNumber":50,"sourceCode":"\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)\n\t\t} else {\n\t\t\treturn \"\", errors.New(\"invalid character in hexadecimal string: \" + string(char))\n\t\t}\n\t\tdecimal = decimal*16 + value\n\t}\n\n\t// Convert the integer to a binary string without using predefined functions\n\tvar binaryBuilder strings.Builder\n\tif decimal == 0 {\n\t\tbinaryBuilder.WriteString(\"0\")\n\t} else {\n\t\tfor decimal > 0 {\n\t\t\tbit := decimal % 2\n\t\t\tif bit == 0 {\n\t\t\t\tbinaryBuilder.WriteString(\"0\")\n\t\t\t} else {\n\t\t\t\tbinaryBuilder.WriteString(\"1\")\n\t\t\t}\n\t\t\tdecimal = decimal / 2\n\t\t}","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/conversion/hexadecimaltobinary.go#L32-L68","documentation":"During the manual parse loop, hexToBinary checks each character against 0-9, A-F and a-f ranges; any other character raises this error with the offending character embedded. It catches single invalid characters that may have passed a weaker or absent upfront validation. The message includes the character via string(char).","triggerScenarios":"A hex string containing an invalid character anywhere after position 0, e.g. \"1A3G\", \"12 34\" (space), \"1A-3F\" (dash), or non-ASCII lookalike characters from Unicode input.","commonSituations":"Hex strings assembled from byte-dump formats with separators, UUID strings with dashes, or data corrupted by encoding conversions (e.g. UTF-16 or smart quotes).","solutions":["Sanitize the input: remove separators (\":\", \"-\", spaces) and validate each rune is in [0-9a-fA-F] before calling.","Use a strict regexp ^[0-9a-fA-F]+$ check to reject bad characters upfront.","Inspect the character named in the error to locate the corruption source in the input pipeline."],"exampleFix":"// before\nout, err := conversion.HexToBinary(\"1A-3F\") // invalid character in hexadecimal string: -\n// after\nclean := strings.NewReplacer(\"-\", \"\", \":\", \"\", \" \", \"\").Replace(raw)\nout, err = conversion.HexToBinary(clean)","handlingStrategy":"validation","validationCode":"var hexRe = regexp.MustCompile(`^[0-9a-fA-F]+$`)\nfunc isCleanHex(s string) bool { return hexRe.MatchString(strings.Map(func(r rune) rune {\n    if r == '-' || r == ':' || r == ' ' { return -1 }\n    return r\n}, s)) }","typeGuard":null,"tryCatchPattern":"out, err := conversion.HexToBinary(hex)\nif err != nil {\n    if strings.HasPrefix(err.Error(), \"invalid character in hexadecimal string:\") {\n        return fmt.Errorf(\"non-hex character found: %w\", err)\n    }\n    return err\n}","preventionTips":["Strip ':', '-', and spaces (UUID/dump formats) before converting.","Validate every character is in [0-9a-fA-F] upfront.","Beware Unicode lookalikes; ensure input is ASCII."],"tags":["conversion","hexadecimal","go","input-validation"],"backgroundTag":"invalid-hex-string","analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}