c# - 将数字对应的chars转换为uint

标签 c# string type-conversion converters uint32

我需要遍历字符串,应该是数字串,并对这些数字进行一些算术运算

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();

http://www.asciitable.com/

关于c# - 将数字对应的chars转换为uint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21833391/

相关文章:

View 组件中的 Javascript

c# - 消失的系统托盘图标

android - 向/从内部文件写入和读取字符串

java - 正则表达式用于从字符串中删除单引号(所有格名词除外)?

c - gtk+: print a float value to an entry widget

c# - AWS .NET Core单元测试加载非默认配置文件

javascript - 网站预览 - 使用 javascript 或服务器端加载网页

java - 如何在运行时用多个分隔符拆分字符串

c++ - (void *)1 是什么意思?

c# - 将 System.Drawing.Icon 转换为 SkiaSharp.SKBitmap