3
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?

4
  • 2
    Unicode code points? What are you trying to achieve exactly? Commented Jul 28, 2016 at 9:13
  • 2
    51,56,50 are asci codes for 3,8,2. You split string to separate chars.. Commented Jul 28, 2016 at 9:13
  • 1
    @BWA, ASCII is so last millenium... Commented Jul 28, 2016 at 9:14
  • 1
    @FrédéricHamidi yes but still works ;-) basic unicode codes maps to asci ;-) Commented Jul 28, 2016 at 9:15

7 Answers 7

13

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.
}
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

1

You need to convert the char back to its numeric value.

Try this:

output += Convert.ToInt32(Char.GetNumericValue(nlst[i]));

Comments

1

[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();
}

Comments

0

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

Comments

0

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

Comments

0

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.