0

I am trying to take a string of digits, and add them to a list (so i can then multiply them together)

public static int METHOD(long n)     
  //25
       {   
        String holder = n.ToString();
       
        List<int> numbers = new List<int>();

        for (int i = 0; i<=holder.Length-1; i++)
        {
            numbers.Add(Convert.ToInt32(holder[i]));
        }
        for (int i = 0; i <= numbers.Count - 1; i++)
        {
            Console.WriteLine("List Position " + [i] + " is: " + numbers[i]);
        }

and you'll get "Number is 50" "Number is 53"

why does it convert 2 to 50? & 5 to 53?

Hearner
  • 2,711
  • 3
  • 17
  • 34
Ben Beart
  • 9
  • 1
  • Did you run a debugger to verify your input? – Omar Abdel Bari Oct 30 '20 at 15:23
  • 9
    The character "2" is not the same as the number 2. The character "2" has the underlying byte value of 50 (dec) 0x32 (hex). if you want the numeric value you're going to have to parse the characters back to integers. – phuzi Oct 30 '20 at 15:23
  • @phuzi is correct. Check out http://www.asciitable.com/ to understand what they're talking about. Character values 48 through 57 are the values for the digits, so you have to do some converting, e.g. `int.Parse(holder[i].ToString())`. – itsme86 Oct 30 '20 at 15:25
  • The issue is you are converting a char to an int32. So it's using the lexographic value of the character rather than it's numeric value. – Omar Abdel Bari Oct 30 '20 at 15:26
  • 2
    Please try to be accurate when asking questions, nothing in shown code output "Number is 50". Also refer to [mcve], The comment `//25` is a poor (unclear) way to show how this method is called. – Sinatr Oct 30 '20 at 15:29

2 Answers2

3

The character "2" is not the same as the number 2. The character "2" has the underlying byte value of 50 (dec) 0x32 (hex). If you want the numeric value you're going to have to parse the characters back to integers.

If you check any 1 of the many ASCII tables out there e.g. Wikipedia, they will confirm the byte values for every ASCII character. Unicode is a little different but in this case there's no difference.

This is why you get "2" => 50 and "5" => 53.

Instead of converting the character to an integer you could parse it:

Int32.Parse(holder[i])

or you could just subtract 48 from the converted value

Convert.ToInt32(holder[i]) - 48

either of these will convert the character code to the correct value, although the subtraction will most likely be quicker in the long run.

phuzi
  • 12,078
  • 3
  • 26
  • 50
0

You are converting the byte value for the char '2' and '5' to an integer. If you change the value to a string and convert it will give you the value you are after.

public static int METHOD(long n)     
//25
{   
    String holder = n.ToString();

    List<int> numbers = new List<int>();

    for (int i = 0; i<=holder.Length-1; i++)
    {
        numbers.Add(Convert.ToInt32(holder[i].ToString()));
    }
    for (int i = 0; i <= numbers.Count - 1; i++)
    {
        Console.WriteLine("List Position " + i + " is: " + numbers[i]);
    }
    return (int)n;
}

example code

Daniel Lord
  • 754
  • 5
  • 18