{"record":{"id":"dd0bb6e4b437094d","repo":"TheAlgorithms/Go","slug":"not-a-valid-binary-string","errorCode":null,"errorMessage":"not a valid binary string","messagePattern":"not a valid binary string","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"conversion/binarytodecimal.go","lineNumber":29,"sourceCode":"// Supported Binary number range is 0 to 2^(31-1).\n// time complexity: O(n)\n// space complexity: O(1)\n\npackage conversion\n\n// Importing necessary package.\nimport (\n\t\"errors\"\n\t\"regexp\"\n)\n\nvar isValid = regexp.MustCompile(\"^[0-1]{1,}$\").MatchString\n\n// BinaryToDecimal() function that will take Binary number as string,\n// and return its Decimal equivalent as an integer.\nfunc BinaryToDecimal(binary string) (int, error) {\n\tif !isValid(binary) {\n\t\treturn -1, errors.New(\"not a valid binary string\")\n\t}\n\tif len(binary) > 32 {\n\t\treturn -1, errors.New(\"binary number must be in range 0 to 2^(31-1)\")\n\t}\n\tvar result, base int = 0, 1\n\tfor i := len(binary) - 1; i >= 0; i-- {\n\t\tif binary[i] == '1' {\n\t\t\tresult += base\n\t\t}\n\t\tbase *= 2\n\t}\n\treturn result, nil\n}\n","sourceCodeStart":11,"sourceCodeEnd":43,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/conversion/binarytodecimal.go#L11-L43","documentation":"BinaryToDecimal validates its string input with the regex ^[0-1]{1,}$ and returns this ad-hoc error when the string contains characters other than 0 or 1 (or is empty). The library creates a fresh errors.New per call rather than an exported sentinel. It guards the bit-accumulation loop which assumes only '0' and '1'.","triggerScenarios":"BinaryToDecimal(\"102\"), BinaryToDecimal(\"\"), BinaryToDecimal(\"0b101\"), or any string with letters, spaces, sign characters, or separators.","commonSituations":"User-supplied input not sanitized (e.g. copy-pasted values with '0b' prefix, underscores, or whitespace), or locale-formatted numeric strings.","solutions":["Validate with regexp ^[01]+$ (after TrimSpace) before calling BinaryToDecimal.","Strip prefixes like \"0b\" and separators like \"_\" from user input first.","Check err from BinaryToDecimal and map it to a user-facing 'enter only 0s and 1s' message."],"exampleFix":"// before\nval, err := conversion.BinaryToDecimal(\"0b1011\") // error: not a valid binary string\n// after\ninput := strings.TrimPrefix(strings.TrimSpace(raw), \"0b\")\nif regexp.MustCompile(`^[01]+$`).MatchString(input) {\n    val, err = conversion.BinaryToDecimal(input)\n}","handlingStrategy":"validation","validationCode":"var binaryRe = regexp.MustCompile(`^[01]+$`)\nfunc isBinary(s string) bool { return binaryRe.MatchString(strings.TrimSpace(s)) }","typeGuard":null,"tryCatchPattern":"val, err := conversion.BinaryToDecimal(input)\nif err != nil {\n    if err.Error() == \"not a valid binary string\" {\n        return fmt.Errorf(\"%q is not binary (only 0/1 allowed)\", input)\n    }\n    return err\n}","preventionTips":["TrimSpace and strip \"0b\" prefixes from user input.","Validate with ^[01]+$ before calling.","Never accept untrusted strings directly from forms/URL params."],"tags":["conversion","binary","go","input-validation"],"backgroundTag":"invalid-binary-string","analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}