{"record":{"id":"0a7c3f62fc6a8b21","repo":"dotnet/wpf","slug":"sr-parsers-illegaltoken-0a7c3f","errorCode":null,"errorMessage":"SR.Parsers_IllegalToken","messagePattern":"SR\\.Parsers_IllegalToken","errorType":"exception","errorClass":"FormatException","httpStatus":null,"severity":"warning","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Parsers.cs","lineNumber":35,"sourceCode":"        private static int ParseHexChar(char c )\n        {\n            int intChar = (int) c;\n\n            if ((intChar >= s_zeroChar) && (intChar <= (s_zeroChar+9)))\n            {\n                return (intChar-s_zeroChar);\n            }\n\n            if ((intChar >= s_aLower) && (intChar <= (s_aLower+5)))\n            {\n                return (intChar-s_aLower + 10);\n            }\n\n            if ((intChar >= s_aUpper) && (intChar <= (s_aUpper+5)))\n            {\n                return (intChar-s_aUpper + 10);\n            }\n            throw new FormatException(SR.Parsers_IllegalToken);\n        }\n\n        private static Color ParseHexColor(string trimmedColor)\n        {\n            int a,r,g,b;\n            a = 255;\n\n            if ( trimmedColor.Length > 7 )\n            {\n                a = ParseHexChar(trimmedColor[1]) * 16 + ParseHexChar(trimmedColor[2]);\n                r = ParseHexChar(trimmedColor[3]) * 16 + ParseHexChar(trimmedColor[4]);\n                g = ParseHexChar(trimmedColor[5]) * 16 + ParseHexChar(trimmedColor[6]);\n                b = ParseHexChar(trimmedColor[7]) * 16 + ParseHexChar(trimmedColor[8]);\n            }\n            else if ( trimmedColor.Length > 5)\n            {\n                r = ParseHexChar(trimmedColor[1]) * 16 + ParseHexChar(trimmedColor[2]);\n                g = ParseHexChar(trimmedColor[3]) * 16 + ParseHexChar(trimmedColor[4]);","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Parsers.cs#L17-L53","documentation":"Parsers.ParseHexChar converts a single character to its hex value and throws FormatException (SR.Parsers_IllegalToken) for any character outside 0-9, a-f, A-F. It is used by ParseHexColor when parsing #-prefixed color strings like #AARRGGBB in WPF XAML.","triggerScenarios":"Parsing a color string containing an invalid hex digit, e.g. #GGHHII, a too-short/odd-length hex string whose character is non-hex, or locale/typo mistakes in XAML color attributes.","commonSituations":"Typo in XAML Color values (e.g. Background=\"#Ff00G0\"), hand-built color strings from user input or config, generated XAML with corrupted values.","solutions":["Fix the color string so it contains only hex characters after '#' (formats: #RGB, #ARGB, #RRGGBB, #AARRGGBB)","Validate with a try-parse before use, e.g. ColorConverter.ConvertFromString in a try/catch or Regex ^#([0-9a-fA-F]{3,8})$","Correct the source XAML/config file where the malformed token originates"],"exampleFix":"// before\nColor c = (Color)ColorConverter.ConvertFromString(\"#GG20FF\"); // FormatException\n// after\nvar m = System.Text.RegularExpressions.Regex.Match(input, \"^#([0-9a-fA-F]{3}|[0-9a-fA-F]{4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$\");\nif (m.Success)\n    Color c = (Color)ColorConverter.ConvertFromString(input);","handlingStrategy":"validation","validationCode":"static readonly Regex HexColorRegex = new Regex(\"^#([0-9a-fA-F]{3}|[0-9a-fA-F]{4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$\");\nbool IsValidHexColor(string s) => s != null && HexColorRegex.IsMatch(s);","typeGuard":null,"tryCatchPattern":"try { color = (Color)ColorConverter.ConvertFromString(input); } catch (FormatException ex) { color = Colors.Transparent; LogParseFailure(input, ex); }","preventionTips":["Match color strings against ^#([0-9a-fA-F]{3,8})$ before parsing","Lint XAML for malformed color attributes","Sanitize user/config-supplied color strings"],"tags":["wpf","parsing","format","color"],"backgroundTag":"invalid-argument-format","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}