dotnet/wpf · warning · FormatException
SR.Parsers_IllegalToken
Error message
SR.Parsers_IllegalToken
What it means
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.
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
Example fix
// before
Color c = (Color)ColorConverter.ConvertFromString("#GG20FF"); // FormatException
// after
var 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})$");
if (m.Success)
Color c = (Color)ColorConverter.ConvertFromString(input); Defensive patterns
Strategy: validation
Validate before calling
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})$");
bool IsValidHexColor(string s) => s != null && HexColorRegex.IsMatch(s); Try / catch
try { color = (Color)ColorConverter.ConvertFromString(input); } catch (FormatException ex) { color = Colors.Transparent; LogParseFailure(input, ex); } Prevention
- Match color strings against ^#([0-9a-fA-F]{3,8})$ before parsing
- Lint XAML for malformed color attributes
- Sanitize user/config-supplied color strings
When it happens
Trigger: 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.
Common situations: Typo in XAML Color values (e.g. Background="#Ff00G0"), hand-built color strings from user input or config, generated XAML with corrupted values.
Understand the failure class
Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.
Related errors
- SR.CompositeFontInvalidUnicodeRange
- SR.Format(SR.InvalidStringCornerRadius, s)
- SR.Format(SR.LengthFormatError, span.ToString())
- errMsg (dynamic: message of caught…
- FileFormatException(new Uri(fileName…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/0a7c3f62fc6a8b21.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Parsers.cs:35
private static int ParseHexChar(char c )
{
int intChar = (int) c;
if ((intChar >= s_zeroChar) && (intChar <= (s_zeroChar+9)))
{
return (intChar-s_zeroChar);
}
if ((intChar >= s_aLower) && (intChar <= (s_aLower+5)))
{
return (intChar-s_aLower + 10);
}
if ((intChar >= s_aUpper) && (intChar <= (s_aUpper+5)))
{
return (intChar-s_aUpper + 10);
}
throw new FormatException(SR.Parsers_IllegalToken);
}
private static Color ParseHexColor(string trimmedColor)
{
int a,r,g,b;
a = 255;
if ( trimmedColor.Length > 7 )
{
a = ParseHexChar(trimmedColor[1]) * 16 + ParseHexChar(trimmedColor[2]);
r = ParseHexChar(trimmedColor[3]) * 16 + ParseHexChar(trimmedColor[4]);
g = ParseHexChar(trimmedColor[5]) * 16 + ParseHexChar(trimmedColor[6]);
b = ParseHexChar(trimmedColor[7]) * 16 + ParseHexChar(trimmedColor[8]);
}
else if ( trimmedColor.Length > 5)
{
r = ParseHexChar(trimmedColor[1]) * 16 + ParseHexChar(trimmedColor[2]);
g = ParseHexChar(trimmedColor[3]) * 16 + ParseHexChar(trimmedColor[4]);View on GitHub (pinned to 81131a70a4)