{"record":{"id":"dad4b5a108d310b2","repo":"TheAlgorithms/Go","slug":"invalid-hexadecimal-string-dad4b5","errorCode":null,"errorMessage":"invalid hexadecimal string","messagePattern":"invalid hexadecimal string","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"conversion/hexadecimaltodecimal.go","lineNumber":38,"sourceCode":"var isValidHexadecimal = regexp.MustCompile(\"^[0-9A-Fa-f]+$\").MatchString\n\n// hexToDecimal converts a hexadecimal string to a decimal integer.\nfunc hexToDecimal(hexStr string) (int64, error) {\n\n\thexStr = strings.TrimSpace(hexStr)\n\n\tif len(hexStr) == 0 {\n\t\treturn 0, fmt.Errorf(\"input string is empty\")\n\t}\n\n\t// Check if the string has a valid hexadecimal prefix\n\tif len(hexStr) > 2 && (hexStr[:2] == \"0x\" || hexStr[:2] == \"0X\") {\n\t\thexStr = hexStr[2:]\n\t}\n\n\t// Validate the hexadecimal string\n\tif !isValidHexadecimal(hexStr) {\n\t\treturn 0, fmt.Errorf(\"invalid hexadecimal string\")\n\t}\n\n\tvar decimalValue int64\n\tfor _, char := range hexStr {\n\t\tvar digit int64\n\t\tif char >= '0' && char <= '9' {\n\t\t\tdigit = int64(char - '0')\n\t\t} else if char >= 'A' && char <= 'F' {\n\t\t\tdigit = int64(char - 'A' + 10)\n\t\t} else if char >= 'a' && char <= 'f' {\n\t\t\tdigit = int64(char - 'a' + 10)\n\t\t} else {\n\t\t\treturn 0, fmt.Errorf(\"invalid character in hexadecimal string: %c\", char)\n\t\t}\n\t\tdecimalValue = decimalValue*16 + digit\n\t}\n\n\treturn decimalValue, nil","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/conversion/hexadecimaltodecimal.go#L20-L56","documentation":"hexToDecimal validates the (prefix-stripped) string against ^[0-9A-Fa-f]+$; characters outside the hex alphabet — including signs, spaces, underscores, or a lone \"0x\" — cause rejection before parsing begins.","triggerScenarios":"Thrown at conversion/hexadecimaltodecimal.go:38 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Sanitize/normalize input: trim, strip 0x/0X prefix, uppercase","Use strconv.ParseInt(s, 16, 64) for standard parsing and error reporting","Show the invalid string to the user for correction"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}