0

I'm trying to convert this string into List of integers

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

I tried this, and many others - but all of the convert this string into I think Unicode-integer-representation of digits, e.g. 7 becomes 55 and 1 becomes 49.. what am I doing wrong? Thank You, for helping.

            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();
Chris Catignani
  • 5,040
  • 16
  • 42
  • 49
apech zzz
  • 25
  • 4

4 Answers4

2

Or if you want to cast, just minus the appropriate ASCII character ('0'). The assumptions are your string is actually just regular ASCII numeric characters

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

Or if you want this faster, you could probably go pointers and unsafe, it will scale a lot better on larger strings

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;
}

Benchmark Environemnt

┌──────────────────┬────────────────────────────────────────────┐
│        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         │
└──────────────────┴──────────┴──────────────────┴──────────────┘

Results

┌── 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 % │
└───────────────────────────────────────────────────────────────────────────────────┘
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
1

There are a couple of static methods in the char class that can help you: char.IsDigit and char.GetNumericValue.

If you have newline characters (or other non-numeric characters) in the string (which it appears to have since it spans multiple lines), then you will first need to filter out any non-digit characters. You can do this by testing them with char.IsDigit.

After that, you can use char.GetNumericValue to get the numeric representation of the char (as opposed to the ASCII value, which is what you're currently getting).

For example:

var result = input.Where(char.IsDigit).Select(char.GetNumericValue).ToList();
Rufus L
  • 36,127
  • 5
  • 30
  • 43
0

Make use of Char.GetNumericValue(x) before adding it into source_list where x is any char from your input list

0

in your example source_list.Select(s => ((int)s)).ToList(); you are converting the char(ascii) equivalent not the value itself

try this

//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();
Gabriel Llorico
  • 1,783
  • 1
  • 13
  • 23
  • FYI: You don't need `ToCharArray()` because the `string` class already implements `IEnumerable` – Rufus L Oct 01 '19 at 15:40