{"record":{"id":"a5cc64eb6247f474","repo":"TheAlgorithms/Go","slug":"invalid-hexadecimal-string","errorCode":null,"errorMessage":"invalid hexadecimal string: ","messagePattern":"invalid hexadecimal string: ","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"conversion/hexadecimaltobinary.go","lineNumber":35,"sourceCode":"\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)\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}","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/conversion/hexadecimaltobinary.go#L17-L53","documentation":"hexToBinary validates the string with isValidHex and returns this error (with the offending input appended) when the string is not valid hexadecimal. The dynamic message embeds the rejected value to ease debugging. It precedes the per-character parse loop.","triggerScenarios":"Calling the hex conversion with strings like \"0x1A\" (prefix not stripped), \"GG\", \"12 34\", or hex with a sign or decimal point.","commonSituations":"Copy-pasting hex values that include the \"0x\"/\"0X\" prefix, whitespace, or byte separators (\":\" or \"-\") from dump tools.","solutions":["Strip a leading \"0x\"/\"0X\" prefix and remove whitespace/separators before calling.","Pre-validate with a regexp ^[0-9a-fA-F]+$ and reject early.","Parse the error message to identify the offending string and fix the input source."],"exampleFix":"// before\nout, err := conversion.HexToBinary(\"0x1A3F\") // invalid hexadecimal string: 0x1A3F\n// after\nclean := strings.TrimPrefix(strings.TrimPrefix(raw, \"0x\"), \"0X\")\nout, err = conversion.HexToBinary(clean)","handlingStrategy":"validation","validationCode":"var hexRe = regexp.MustCompile(`^[0-9a-fA-F]+$`)\nfunc isHex(s string) bool {\n    s = strings.TrimPrefix(strings.TrimPrefix(s, \"0x\"), \"0X\")\n    return hexRe.MatchString(s)\n}","typeGuard":null,"tryCatchPattern":"out, err := conversion.HexToBinary(hex)\nif err != nil {\n    if strings.HasPrefix(err.Error(), \"invalid hexadecimal string:\") {\n        return fmt.Errorf(\"bad hex input: %w\", err)\n    }\n    return err\n}","preventionTips":["Strip 0x/0X prefixes from dump output.","Remove whitespace and separators before parsing.","Validate with ^[0-9a-fA-F]+$ first."],"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"}