1. ## Conversion/String Problem

I was solving Project Eular-Problem 56 ( Problem 56 - Project Euler ), its relatively easy problem.But i encountered a small issue.I was so frustrated cause i couldn't find solution, so i have to google it, eventually i found the problem, and i need an explanation.
The problem was in sum of digits of digit:
Code:
```   for (int j = 0; j < 10000; j++)
{
s = Pdigits[j];
for (int i = 0; i < s.Length; i++)
array[j] += Convert.ToInt32(s[i]);

}```
So here is my code (array Pdigits represent array of numbers), when i sum digits of eg. 99^99 i get 10440, and it should be 936.I just couldn't figure why.I found out that the problem was in
Code:
`array[j] += Convert.ToInt32(s[i]);`
(of course).
Here is the solution.
Code:
```    for (int j = 0; j < 10000; j++)
{
s = Pdigits[j];
char[] k = s.ToCharArray();
for (int i = 0; i < s.Length; i++)
array[j] += Convert.ToInt32(k[i].ToString());

}```
With using char array k, and then converting it to string i get good output, my question is why?

2. Convert.ToInt32(char) doesn't do what you think it does, and s[i] produces a value of char type. It gives you the integer value of the character, not the integer representation of a digit, which is what I assume you're trying to do. Convert.ToInt32(string) does do what you want, which is why converting the character to a string gives you the correct result. You could get the same result with this:
Code:
```for (int j = 0; j < 10000; j++)
{
s = Pdigits[j];

for (int i = 0; i < s.Length; i++)
array[j] += Convert.ToInt32(s[i].ToString());
}```
Or the more classic trick:
Code:
```for (int j = 0; j < 10000; j++)
{
s = Pdigits[j];

for (int i = 0; i < s.Length; i++)
array[j] += s[i] - '0';
}```

3. Originally Posted by Prelude
Convert.ToInt32(char) doesn't do what you think it does, and s[i] produces a value of char type. It gives you the integer value of the character, not the integer representation of a digit, which is what I assume you're trying to do. Convert.ToInt32(string) does do what you want, which is why converting the character to a string gives you the correct result. You could get the same result with this:
Code:
```for (int j = 0; j < 10000; j++)
{
s = Pdigits[j];

for (int i = 0; i < s.Length; i++)
array[j] += Convert.ToInt32(s[i].ToString());
}```
Thank you i understand it now.
What i did as a test is printing s[i] (before i made this thread), and i was confused cause i didn't converted it to int, and still i would get numbers printed, which couldn't work if it was a string. Thanks again mate.