简体   繁体   English

无法将字符串转换为int:'System.FormatException'

[英]Can't convert string to int: 'System.FormatException'

I'm having trouble converting a string to a integer, my program is failing on this line 我在将字符串转换为整数时遇到问题,我的程序在此行失败

int newS = int.Parse(s);

With message: 带有消息:

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll mscorlib.dll中发生了'System.FormatException'类型的未处理异常

The number I'm expecting back from my program is rather large. 我期望程序返回的数字很大。 Below is the total program: 下面是总程序:

int math = (int)Math.Pow(2,1000);
string mathString = math.ToString();

List<string> list = new List<string>();

char[] ch = mathString.ToCharArray();
int result = 0;

foreach (char c in mathString)
{
    string newC = c.ToString();
    list.Add(newC);
    //Console.WriteLine(newC);
}

foreach (string s in list)
{
    int newS = int.Parse(s);
    result += newS;

}

Console.Write(result);
Console.ReadLine();

You answered your own question. 你是在自问自答。 An int can only store numbers up to 2147483648 and an unsigned int up to 4294967296. try to use an ulong instead. 一个int最多只能存储2147483648,而一个无符号的int最多可以存储4294967296。请尝试使用ulong。 I'm not sure about this but maybe a signed long may work. 我对此不太确定,但也许签了字可能会奏效。

EDIT: actually, in the msdn page it says this: 编辑:实际上,在msdn页面中它说:

If the value represented by an integer literal exceeds the range of ulong, a compilation error will occur. 如果由整数文字表示的值超出ulong的范围,则会发生编译错误。

So probably you need a double. 因此,可能您需要加倍。

Math.Pow(2, 1000) returns -2147483648 . Math.Pow(2,1000)返回-2147483648

So you'll end up with 11 items in your list, the first one being "-". 因此,您最终将在列表中找到11个项目,第一个为“-”。

You can't convert a minus sign to int. 您不能将减号转换为int。

In all of the types of all languages are a limit on the numbers that you can save. 在所有语言的所有类型中,可以保存的数字都有限制。 The int of c# is -2,147,483,648 to 2,147,483,647. c#的int是-2,147,483,648至2,147,483,647。 https://msdn.microsoft.com/en-us/library/5kzh1b5w.aspx https://msdn.microsoft.com/zh-CN/library/5kzh1b5w.aspx

Math.Pow returns a double, when you want to cast it to int your variable gets the value 0 Math.Pow返回一个double值,当您要将其强制转换为int时,变量的值为0

Math.Pow(2,1000) returns: 1.07150860718627E+301. Math.Pow(2,1000)返回:1.07150860718627E + 301。

If you use the double format you will try to cast the . 如果您使用double格式,则将尝试投射。 and the E and the +, that are not a int then you can't use a int to save it. 和E和+,它们不是int,则不能使用int来保存它。

that returns the FormatException that are answered here: int.Parse, Input string was not in a correct format 返回在此处回答的FormatException: int.Parse,输入字符串的格式不正确

Maybe you can try this: 也许您可以尝试以下方法:

int newS; 国际新闻;

if (!int.TryParse(Textbox1.Text, out newS)) newS= 0; 如果(!int.TryParse(Textbox1.Text,out newS))newS = 0;

result +=newS; 结果+ = newS;

But will not use the 301 digits of the solution of 2^1000. 但不会使用2 ^ 1000解决方案的301位数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将字符串数组转换为int数组&#39;System.FormatException&#39; - Convert string array to int array 'System.FormatException' System.FormatException:“无法将参数值从 String 转换为 Int32。” - System.FormatException: 'Failed to convert parameter value from a String to a Int32.' 字符串上Int32上的System.FormatException - System.FormatException on Int32 from string 尝试将String转换为Double时发生System.FormatException - System.FormatException when trying to convert String to Double 尝试将字符串转换为整数会导致System.FormatException类型的错误 - Trying to convert a string to integer causes a error of type System.FormatException MVC将Base64字符串转换为Image,但是...... System.FormatException - MVC Convert Base64 String to Image, but … System.FormatException System.FormatException:字符串未被识别为有效的DateTime - System.FormatException: String was not recognized as a valid DateTime System.FormatException:字符串未被识别为有效的DateTime - System.FormatException: String was not recognized as a valid DateTime 无法将字符串识别为有效的DateTime(System.FormatException)? - String was not recognized as a valid DateTime(System.FormatException)? System.FormatException:输入字符串的格式不正确 - System.FormatException:Input string was not in a correct format
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM