Reputation: 33
static void Main(string[] args)
{
int num = 382;
int output = 0;
char[] nlst = num.ToString().ToCharArray();
for (int i = 0; i < nlst.Length; i++)
{
output += nlst[i];
}
Console.WriteLine(output);
Console.ReadLine();
}
The output result is 157, Actually it should be 13.With Dedbugging I found the 3 elements of char[] nlst like this :
[0]51'3', [1]56'8', [2]50'2'
Why? What's the meaning of 51,56,50?
Upvotes: 3
Views: 28370
Reputation: 31
The correct solution is
output += Convert.ToInt32(nlst[i]) - '0';
or briefly
output += nlst[i] - '0';
or, as an alternative
output += Convert.ToInt32(Convert.ToString(nlst[i]));
Please note: As well as C++, C# Convert.ToInt32 converts chars to basic Unicode (i.e. good old Ascii).
Upvotes: 0
Reputation: 109557
You're assuming that the char value of '0'
is 0. It is not; it is in fact the UTF16 value of '0'
which is 48.
So you are adding together the UTF16 values of the characters '3'
, '8'
and '2'
, i.e. 51, 56 and 50.
Note that if your aim is to add together all the digits of an integer, the best approach is to avoid converting to a string completely, like so:
int num = 382;
// Compute the sum of digits of num
int total = 0;
while (num > 0)
{
total += num%10;
num /= 10;
}
Console.WriteLine(total);
However if you just want to know how to get your version working, just subtract '0'
from each character before adding the codes together. That will convert '0'
to 0, '1'
to 1, etc:
for (int i = 0; i < nlst.Length; i++)
{
output += nlst[i] - '0'; // Subtract '0' here.
}
Upvotes: 13
Reputation: 61
[EDITED] Char is unicode 16 (https://msdn.microsoft.com/en-us/library/x9h8tsay.aspx). You can use int.Parse
static void Main(string[] args)
{
int num = 382;
int output = 0;
char[] nlst = num.ToString().ToCharArray();
for (int i = 0; i < nlst.Length; i++)
{
output += int.Parse(nlst[i].ToString());
}
Console.WriteLine(output);
Console.ReadLine();
}
Upvotes: 0
Reputation: 49095
You need to convert the char
back to its numeric value.
Try this:
output += Convert.ToInt32(Char.GetNumericValue(nlst[i]));
Upvotes: 0
Reputation: 137
Your output variable is defined as an integer. If you add (+=) a char to an integer, it's unicode value will be added to the output variable.
static void Main(string[] args)
{
int num = 382;
int output = 0;
char[] nlst = num.ToString().ToCharArray();
for (int i = 0; i < nlst.Length; i++)
{
output += Convert.ToInt32(nlst[i]);
}
Console.WriteLine(output);
Console.ReadLine();
}
Upvotes: 0
Reputation: 443
Those numbers are the char codes of the numbers. If you just want to add up the value of each Digit, you can subtract 48 from each char value to turn it into ist number value
for (int i = 0; i < nlst.Length; i++)
{
output += nlst[i] - 48;
}
Upvotes: 0
Reputation: 7579
Those are the Unicode values for '3', '8' and '2' respectively.
To convert an Unicode value (eg. 51) to the integer representation of the character represented by the Unicode value (eg. 3), use the Convert.ToInt32(char)
method.
Upvotes: 2