int.Parse()与领先的零 [英] int.Parse() with leading zeros

查看:254
本文介绍了int.Parse()与领先的零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何prevent的code从下方投掷出现FormatException 。我希望能够分析具有前导零的字符串转换为整数。有没有干净的方式做到这一点?

 字符串值=01;
INT I = int.Parse(值);
 

解决方案

您code运行对我来说,没有一个出现FormatException (一旦你正确把握方法)

 字符串值=01;
INT I = int.Parse(值);
 

但这环的老钟;一个问题,我几年前,微软接受反的Windows(不是.NET)的本地化组件的错误。为了测试是否你看到的是,运行此code,让我们知道你是否得到一个出现FormatException:

 字符串值=0; //只是一个零
INT I = int.Parse(值);
 

修改:这里是从新闻组我的文章,从早在2007年看症状是否符合你的

  

有关参考,下面就是我们的发现。   受影响的机器有坏数据   注册表值   [HKEY_CURRENT_USER \控制面板   \国际\] sPositiveSign。   通常情况下,这个值是一个空   REG_SZ(空值终止字符串)。在   这种情况下,该字符串缺少的   终止符。这混淆了API   功能GetLocaleInfoW(),导致它   认为0(ASCII数字零)   是积极的迹象当前   区域(通常它应该是'+')。   这导致各种混乱的。

     

您可以验证这一点为自己与   REGEDIT.EXE:通过打开寄存器值   该值右击并   选择修改二进制数据。您   应该看到在右侧两个点   (重presenting空终止)。   如果您没有看到点,你的影响。   通过将终止对其进行修复(4   零)。

     

您也可以检查的价值   CultureInfo.CurrentCulture.NumberFormat.PositiveSign;   它应该是+

     

这是在Windows本地化错误   API,而不是类库。该章   值需要被检查的   终止符。他们正在寻找它。

...和这里的有关问题在Microsoft Connect上报告:

How do I prevent the code below from throwing a FormatException. I'd like to be able to parse strings with a leading zero into ints. Is there a clean way to do this?

string value = "01";
int i = int.Parse(value);

解决方案

Your code runs for me, without a FormatException (once you capitalize the method properly):

string value = "01";
int i = int.Parse(value);

But this does ring an old bell; a problem I had years ago, which Microsoft accepted as a bug against localization components of Windows (not .NET). To test whether you're seeing this, run this code and let us know whether you get a FormatException:

string value = "0"; // just a zero
int i = int.Parse(value);

EDIT: here's my post from Usenet, from back in 2007. See if the symptoms match yours.

For reference, here's what we found. The affected machine had bad data for the registry value [HKEY_CURRENT_USER\Control Panel \International\sPositiveSign]. Normally, this value is an empty REG_SZ (null-terminated string). In this case, the string was missing its terminator. This confused the API function GetLocaleInfoW(), causing it to think that '0' (ASCII number zero) was the positive sign for the current locale (it should normally be '+'). This caused all kinds of havoc.

You can verify this for yourself with regedit.exe: open that reg value by right-clicking on the value and selecting 'Modify Binary Data'. You should see two dots on the right (representing the null terminator). If you see no dots, you're affected. Fix it by adding a terminator (four zeros).

You can also check the value of CultureInfo.CurrentCulture.NumberFormat.PositiveSign; it should be '+'.

It's a bug in the Windows localization API, not the class libs. The reg value needs to be checked for a terminator. They're looking at it.

...and here's a report on Microsoft Connect about the issue:

这篇关于int.Parse()与领先的零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆