M
Michael Petrotta
I have an odd problem affecting some of my customer's systems. I've
reduced it down to a simple, but bedeviling issue: Int32.Parse is
failing to convert the string "0", on some systems.
On those systems, the Framework throws a FormatException ("Input string
was not in a correct format."). Convert.ToInt32 does the same thing.
Every other single-digit string ("1", "2",..."9") works fine, as does
any two-digit string with a leading 0 ("03", "04", etc). And yes, I've
checked that I'm using a zero, and not the letter "O".
Here's how I reproduced it:
using System;
namespace ParseTest
{
class Program
{
static void Main()
{
int i = Convert.ToInt32("0"); // throws a FormatException on some
systems
Console.WriteLine("parsed: " + i);
Console.Read();
}
}
}
I can work around this by calling Convert.ToInt32 with a base argument:
Convert.ToInt32("0", 10). When I reflect on that method, I see that
Convert.ToInt32(string) follows a completely different code path than
does Convert.ToInt32(string, int). The one-arg overload calls directly
into Int32.Parse, and the two-arg overload calls
ParseNumbers.StringToInt. The one-arg overload does an explicit parse
test (Number.ParseNumber) that fails, and the two-arg overload calls
into an internal call that I can't follow.
Specifying the numeric base will fix some of our problems, but not all;
some of these parse errors are generated by the same calls deep inside
third-party components.
This only happens on .NET 2.0. 1.1 parses this string on all systems
without trouble.
This sample code will probably work for you; it's only failing on some
systems. Guessing that this might be a localization issue, I checked
one machine's "Regional and Language Options" control panel. Every
standard and advanced setting looked fine, and the format was set to
"English (United States)". .Net reports the current culture set to
en-US.
Here's a funny bit - when I switched the control panel's format from US
to something else, and back to US, the problem resolved itself. No
more problems parsing zero. No amount of further fiddling with the
control panel would bring the problem back. I took a before-and-after
snapshot of what I think was the relevant section of the registry
(HKEY_CURRENT_USER\Control Panel\International). No changes.
So I have a way to resolve this on clients experiencing this error -
twiddle their culture setting. It seems like some culture bits got
messed up, which I would expect to see on an isolated machine but not
on the number I've seen here. Has anyone else experienced this, or
have any idea why it's happening? Seems like a bug to me - any reason
not to file this?
Thanks for any help,
Michael
reduced it down to a simple, but bedeviling issue: Int32.Parse is
failing to convert the string "0", on some systems.
On those systems, the Framework throws a FormatException ("Input string
was not in a correct format."). Convert.ToInt32 does the same thing.
Every other single-digit string ("1", "2",..."9") works fine, as does
any two-digit string with a leading 0 ("03", "04", etc). And yes, I've
checked that I'm using a zero, and not the letter "O".
Here's how I reproduced it:
using System;
namespace ParseTest
{
class Program
{
static void Main()
{
int i = Convert.ToInt32("0"); // throws a FormatException on some
systems
Console.WriteLine("parsed: " + i);
Console.Read();
}
}
}
I can work around this by calling Convert.ToInt32 with a base argument:
Convert.ToInt32("0", 10). When I reflect on that method, I see that
Convert.ToInt32(string) follows a completely different code path than
does Convert.ToInt32(string, int). The one-arg overload calls directly
into Int32.Parse, and the two-arg overload calls
ParseNumbers.StringToInt. The one-arg overload does an explicit parse
test (Number.ParseNumber) that fails, and the two-arg overload calls
into an internal call that I can't follow.
Specifying the numeric base will fix some of our problems, but not all;
some of these parse errors are generated by the same calls deep inside
third-party components.
This only happens on .NET 2.0. 1.1 parses this string on all systems
without trouble.
This sample code will probably work for you; it's only failing on some
systems. Guessing that this might be a localization issue, I checked
one machine's "Regional and Language Options" control panel. Every
standard and advanced setting looked fine, and the format was set to
"English (United States)". .Net reports the current culture set to
en-US.
Here's a funny bit - when I switched the control panel's format from US
to something else, and back to US, the problem resolved itself. No
more problems parsing zero. No amount of further fiddling with the
control panel would bring the problem back. I took a before-and-after
snapshot of what I think was the relevant section of the registry
(HKEY_CURRENT_USER\Control Panel\International). No changes.
So I have a way to resolve this on clients experiencing this error -
twiddle their culture setting. It seems like some culture bits got
messed up, which I would expect to see on an isolated machine but not
on the number I've seen here. Has anyone else experienced this, or
have any idea why it's happening? Seems like a bug to me - any reason
not to file this?
Thanks for any help,
Michael