2

I want an solution to convert an input int say 010 to list of int {0,1,0}. Below is code I tried, works until 0 is encountered.

Int num = 010;
List<int> listOfInt = new List<int>();
While(num > 0)
listOfInt.Add(num%10);
num / = 10;

I just want to split entered int and convert it to list of int. LINQ is fine if that could be efficient!

A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
parth joshi
  • 157
  • 1
  • 4
  • 13
  • LINQ does not deal with this – Hemal Feb 10 '17 at 12:12
  • 5
    `010` can never be an int btw, `010` is `10` when represented as an integer, lead zeros are irrecoverable. – Alex K. Feb 10 '17 at 12:12
  • you probably missing `{` and `}` – Valentin Feb 10 '17 at 12:12
  • If you want this code fixed, explain your problem very explicitly (what is _"works until 0 is encountered"_? Which 0? What do you expect to see, and what do you actually see?). If you just want any code that does this, see [How to split a number into individual digits in c#?](http://stackoverflow.com/questions/4808612/how-to-split-a-number-into-individual-digits-in-c). – CodeCaster Feb 10 '17 at 12:12
  • You have to loop through string length and add each one – Hemal Feb 10 '17 at 12:12
  • 1
    an Integer 010 will always be saved as 10. So if you want to save "010" you need a string – rkgghz Feb 10 '17 at 12:13
  • There is no difference between `010` and `10` either in computer arithmetic or real life. Zero is zero. If you want to convert the number to a specific string format and extract the characters,just do so: `ToString("000").Select(c=>c-48).ToList()`. The `c-48` takes advantage of the fact that characters are essentially ints, and digits start from 0 upwards – Panagiotis Kanavos Feb 10 '17 at 12:13

4 Answers4

9

As others already mentioned 010 is identical to 10 when having parsed as int. However you could have your number as string coming from a console-input for example.

string myNumber = "010";

This can be split on every character quite easy using LINQ:

var intList = myNumber.Select(x => Convert.ToInt32(x.ToString())).ToList();

As every character is internally an int where '0' equals 49 you have to convert every single character to a string before which is done by using ToString.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
3
Console.WriteLine("Enter a number:")
var input = Console.ReadLine();

List<int> result = input.Select(c => int.Parse(c.ToString())).ToList();
nvoigt
  • 75,013
  • 26
  • 93
  • 142
2

There is no difference between 010 and 10 either in computer arithmetic or real life. Zero is zero.

If you want to convert the number to a specific string format and extract the characters, perform the same steps as the statement:

10.ToString("000").Select(c=>c-48).ToList();

The result is a list with the numbers 0,1,0.

The expression c-48 takes advantage of the fact that characters are essentially ints, and digits start from 0 upwards. So 48 is 0, 1 is 49 etc.

If the input is a string, eg "10" you'll have to pad it with 0s up to the desired length:

"10".PadLeft(3,'0').Select(c=>c-48).ToList()

The result will be 0,1,0 again.

If, after all, you only want to retrieve characters from a paddes string, you only need padding, as a String is an IEnumerable. You can copy the characters to an array with String.ToCharArray() or to a List as before:

"10".PadLeft(3,'0').ToList()
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
1
string num = "010";
List<int> listOfInt = new List<int>();
foreach(char item in num)
{
  listOfInt.Add(Convert.ToInt32(item.ToString()));
}
andy
  • 5,979
  • 2
  • 27
  • 49
  • 2
    No cast means you would get a list of int character codes – Alex K. Feb 10 '17 at 12:17
  • don't u need a conversion from char to int before adding it to list. iirc, there is no implicit conversion from `char` to `int` – Abdul Hameed Feb 10 '17 at 12:20
  • 2
    Implicitly `(int)'1'` is 49 – Alex K. Feb 10 '17 at 12:23
  • Convert.ToInt32(item.ToString()) it works item.ToString() will give characters not ascii values – andy Feb 10 '17 at 12:24
  • @andy or, since characters are integers, subtract 48. No allocations, conversions or parsing. `'1'-48` returns `1` – Panagiotis Kanavos Feb 10 '17 at 12:25
  • @PanagiotisKanavos Or, even `item - '0'`. That way you avoid a magic number `48`. – PC Luddite Feb 10 '17 at 12:35
  • @PCLuddite it's not a magic number. `0` *is* 48 – Panagiotis Kanavos Feb 10 '17 at 12:38
  • @PanagiotisKanavos yes, but in my opinion it's more clear what you're trying to do if you subtract character `'0'` rather than integer `48`. I like many others do not have the ascii table memorized. – PC Luddite Feb 10 '17 at 12:43
  • @PanagiotisKanavos I think you'll also help give more evidence to your point that `char`s are promoted to integers by doing this. Otherwise the assumption could be made that `char` are promoted to integers only when one of the operand is an integer as well. – PC Luddite Feb 10 '17 at 12:45