-1

フォーマットと変換に問題があります。

私はすべての回避策を試しましたが、何もしませんでした。

エラーがあると思われるコードスニペット

label_Map.Text = message.Substring(21, 3);
label_Sys.Text = message.Substring(15, 3);
label_Dia.Text = message.Substring(18, 3);
label_Pulse.Text = message.Substring(26, 3);

SaveData(
    Int32.Parse(message.Substring(15, 3)),
    Int32.Parse(message.Substring(18, 3)),
    Int32.Parse(message.Substring(26, 3)));

入力文字列の例

S1;A0;C03;M00;P120080100;R075;T0005;;D2

エラーコードの終わり

InnerException: System.FormatException    
Message=Input string was not in a correct format.   
Source=mscorlib   
StackTrace:  
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number,  NumberFormatInfo info, Boolean parseDecimal)  
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)  
at NIBP2PC.Form1.display(String message) in C:\Users\bazinga\Desktop\spiediena_merisana\NIBP2PC_c#\NIBP2PC\Form1.cs:line 427
4

3 に答える 3

0

あなたの部分文字列はおそらく「P12」や「00;」などを返しています。これは解析できません。したがって、エラー メッセージは正しいです (というか、問題が実際に何であるかを示しています)。送信する文字列を修正するまでデータを取得することはできませんが、これを行うより良い方法は、「TryParse」を使用することです

int myInt;
if (!Int32.TryParse(myString, out myInt)) throw new Exception() //or something more reasonable
于 2013-05-14T16:01:29.280 に答える