我需要遍历字符串,应该是数字串,并对这些数字进行一些算术运算
for (int i = data.Length - 1; i >= 0; --i)
{
uint curDigit;
//Convert to uint the current rightmost digit, if convert fails return false (the valid data should be numeric)
try
{
curDigit = Convert.ToUInt32(data[i]);
//arithmetic ops...
}
catch
{
return false;
}
我使用以下输入数据字符串对其进行测试。
“4000080706200002”
对于 i = 15
,对应于最右边的数字 2
,我得到 50
作为
curDigit = Convert.ToUInt32(data[i]);
谁能解释一下哪里出了问题,以及如何解决这个问题
最佳答案
50 是 '2'
的 ascii 码。你需要的是 '2' - '0'
(50-48)
byte[] digits = "4000080706200002".Select(x => (byte)(x - '0')).ToArray();
关于c# - 将数字对应的chars转换为uint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21833391/