以下问题在C#中(在VS社区2015中工作):
首先,我相当新的C#,所以请问如果这个问题很容易解决。
我有一个接触传感器给我一串数字(长度测量)。 我用SystemPort方法读取它们,并将它们切成我需要的子字符串(因为字符串的开头,“SR00002”对我来说是无用的)。
最后,我最终得到了一个字符串:“000.3422”或“012.2345”。 现在我想将该字符串转换为一个可以使用的实体int变量,意味着从中减去值等。
Bsp:我想计算012.234 - 000.3422(或者,而不是。但我可以事先改变它)
我已经尝试过Parse和ConvertToInt(在迭代字符串时)但是endresult总是一个字符串。
string b = serialPort2.ReadLine(); string[] b1 = Regex.Split(b, "SR,00,002,"); string b2 = b1[1].Substring(1); foreach (char c in b2) { Convert.ToInt32(c); } textBox2.Text = b2 + b2.GetType();我知道,当b2是int时,它不能被打印在文本框中,但后来生病了。
当所有内容都相应转换后,生病将转换外包给自己的方法=)
GetType仅用于测试,并且只显示System.String(我不想)。 帮助会很大的帮助。 我也浏览了搜索功能和谷歌,但无法找到任何帮助。 我希望任何可能的帮手都有美好的一天,克里斯。
following problem in C# (working in VS Community 2015):
First off, i fairly new to C#, so excuse me if that question would be an easy fix.
I have a contact sensor giving me a string of numbers (length measurement). I read them with the SystemPort Methods and cut them down to the numbers that i need with substring (as the beginning of the string, the "SR00002" is useless to me).
In the end i end up with a string like : "000.3422" or "012.2345". Now i want to convert that string to one solid int-variable that i can work with, meaning subtract values from and such.
Bsp: I want to calculate 012.234 - 000.3422 (or , instead of . but i could change that beforehand)
I already tried Parse and ConvertToInt (while iterating through the string) but the endresult is always a string.
string b = serialPort2.ReadLine(); string[] b1 = Regex.Split(b, "SR,00,002,"); string b2 = b1[1].Substring(1); foreach (char c in b2) { Convert.ToInt32(c); } textBox2.Text = b2 + b2.GetType();I know that when b2 will be int it can not be printed in the Textbox but ill take care of that later.
When everything is converted accordingly, ill outsource the conversion to its own method =)
The GetType is just for testing and as said shows only System.String (which i dont want). Help would be much appreaciated. I also browsed the searchfunction and google but couldnt find anything of help. I wish any possible helpers a nice day, mfg Chris.
最满意答案
使用int.Parse
int.Parse( “123”)
use the int.Parse
int.Parse("123")
更多推荐
发布评论