Well, since number[i]
is of type char
you can convert it into corresponding int
either by (the most general case)
int result = (int) char.GetNumericValue(number[i]);
please notice, that char.GetNumericValue
returns double
, it's actual for, say, '⅜'
character (3/8
corrsponds to 0.375
); or if you work with ['0'..'9']
range only, all you have to do is to subtract '0'
:
int result = number[i] - '0';
So far your code can be implemented as
// Math.Min - number.Length and number2.Length have diffrent size
// - 1 - strings are zero-based [0..Length - 1]
for (i = Math.Min(number.Length, number2.Length) - 1; i >= 0; i--) {
int save = number[i] + number2[i] - 2 * '0';
...
}
probably you want to right align both strings:
number = "124235245423523423"
+
number2 = "3423525232332325423"
------------------------------
354... ...846
in this case you have to modify for
loop:
for (int i = 0; i < Math.Max(number.Length, number2.Length); ++i) {
int sum =
(i < number.Length ? number[number.Length - i - 1] : '0') +
(i < number2.Length ? number2[number2.Length - i - 1] : '0') -
2 * '0';
...
}