【问题标题】:How to convert string of digits to List<int> in C#? [duplicate]如何在 C# 中将数字字符串转换为 List<int>? [复制]
【发布时间】:2020-01-30 07:29:37
【问题描述】:

我正在尝试将此字符串转换为整数列表

“73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450"

我试过这个,还有很多其他的——但是所有的把这个字符串转换成我认为的 Unicode 整数表示的数字,例如7变成55,1变成49..我做错了什么? 谢谢你的帮助。

            List<char> source_list = new List<char>();            
            foreach (char x in source)
            {
                source_list.Add(x);
            }
            List<int> new_list = source_list.Select(s => ((int)s)).ToList();

【问题讨论】:

  • 添加了一个答案并解释了为什么您的 ((int)s) 不起作用

标签: c# list


【解决方案1】:

或者如果你想cast,只需减去相应的ASCII 字符('0')。假设您的 string 实际上只是常规 ASCII 数字字符

var result = someString.Select(ch => ch -'0').ToList();

或者如果你想要更快,你可以使用 pointersunsafe,它会在更大的字符串上更好地扩展

private unsafe static int[] SpeedConvert(string source)
{
   var result = new int[source.Length];
   fixed(int* pResult = result)
      fixed (char* pSource = source)
         for (var i = 0; i < source.Length; i++)
            *(pResult + i) = *(pSource + i) - '0';
    return result;
}

基准环境

┌──────────────────┬────────────────────────────────────────────┐
│        Test Mode │ Release (64Bit)                            │
│   Test Framework │ .NET Framework 4.7.1 (CLR 4.0.30319.42000) │
╞══════════════════╪════════════════════════════════════════════╡
│ Operating System │ Microsoft Windows 10 Pro                   │
│          Version │ 10.0.18362                                 │
├──────────────────┼────────────────────────────────────────────┤
│       CPU System │ Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz    │
│  CPU Description │ Intel64 Family 6 Model 158 Stepping 9      │
├──────────────────┼──────────┬──────────────────┬──────────────┤
│  Cores (Threads) │ 4 (8)    │     Architecture │ x64          │
│      Clock Speed │ 3600 MHz │        Bus Speed │ 100 MHz      │
│          L2Cache │ 1 MB     │          L3Cache │ 8 MB         │
└──────────────────┴──────────┴──────────────────┴──────────────┘

结果

┌── Standard input ─────────────────────────────────────────────────────────────────┐
│ Value        │    Average │    Fastest │    Cycles │ Garbage │ Test │        Gain │
├── Scale 2 ─────────────────────────────────────────────────────────── 0.741 sec ──┤
│ SpeedConvert │   3.548 µs │   1.000 µs │  17.179 K │ 0.000 B │ N/A  │      0.00 % │
│ Select       │   5.050 µs │   1.800 µs │  22.411 K │ 0.000 B │ N/A  │    -42.34 % │
├── Scale 20 ────────────────────────────────────────────────────────── 0.731 sec ──┤
│ SpeedConvert │   3.871 µs │   1.000 µs │  18.084 K │ 0.000 B │ N/A  │      0.00 % │
│ Select       │   5.555 µs │   2.500 µs │  24.377 K │ 0.000 B │ N/A  │    -43.50 % │
├── Scale 200 ───────────────────────────────────────────────────────── 0.754 sec ──┤
│ SpeedConvert │   3.817 µs │   1.100 µs │  18.298 K │ 0.000 B │ N/A  │      0.00 % │
│ Select       │   9.532 µs │   5.900 µs │  38.478 K │ 0.000 B │ N/A  │   -149.72 % │
├── Scale 2,000 ─────────────────────────────────────────────────────── 0.811 sec ──┤
│ SpeedConvert │   5.148 µs │   1.900 µs │  22.722 K │ 0.000 B │ N/A  │      0.00 % │
│ Select       │  46.030 µs │  40.300 µs │ 170.014 K │ 0.000 B │ N/A  │   -794.11 % │
├── Scale 20,000 ────────────────────────────────────────────────────── 1.509 sec ──┤
│ SpeedConvert │  15.107 µs │  12.400 µs │  58.771 K │ 0.000 B │ N/A  │      0.00 % │
│ Select       │ 486.696 µs │ 479.300 µs │   1.755 M │ 0.000 B │ N/A  │ -3,121.76 % │
└───────────────────────────────────────────────────────────────────────────────────┘

【讨论】:

    【解决方案2】:

    char 类中有几个 static 方法可以帮助您:char.IsDigitchar.GetNumericValue

    如果字符串中有换行符(或其他非数字字符)(它似乎有,因为它跨越多行),那么您首先需要过滤掉任何非数字字符。您可以通过使用char.IsDigit 测试它们来做到这一点。

    之后,您可以使用char.GetNumericValue 来获取char 的数字表示(而不是ASCII 值,这是您当前获得的值)。

    例如:

    var result = input.Where(char.IsDigit).Select(char.GetNumericValue).ToList();
    

    【讨论】:

      【解决方案3】:

      在将 Char.GetNumericValue(x) 添加到 source_list 之前使用它,其中 x 是输入列表中的任何字符

      【讨论】:

        【解决方案4】:

        在你的例子中 source_list.Select(s =&gt; ((int)s)).ToList(); 您正在转换 char(ascii) 等价物而不是值本身

        试试这个

        //assume source has no white space
        //adding .ToString() to char will convert the char to the real value
        var listOfCharConvertedToInt = source.ToCharArray()
                                             .Select(ch => Convert.ToInt32(ch.ToString()))
                                             .ToList();
        

        【讨论】:

        • 仅供参考:您不需要ToCharArray(),因为string 类已经实现了IEnumerable&lt;char&gt;
        猜你喜欢
        • 2010-09-17
        • 2013-11-12
        • 1970-01-01
        • 2012-02-03
        • 2012-07-04
        • 2011-06-18
        • 2012-07-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多