Thread: Conversion/String Problem

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    16

    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. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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';
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    16
    Quote Originally Posted by Prelude View Post
    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.
    Last edited by Stevan; 03-02-2013 at 12:45 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Conversion
    By r_james14 in forum C Programming
    Replies: 2
    Last Post: 02-29-2012, 07:59 AM
  2. String Conversion
    By 6kyAngel in forum C Programming
    Replies: 2
    Last Post: 03-17-2009, 07:59 AM
  3. String conversion problem
    By Mr.Bit in forum C Programming
    Replies: 9
    Last Post: 03-04-2008, 04:39 PM
  4. string object to null-terminating string conversion
    By sayz0 in forum C++ Programming
    Replies: 2
    Last Post: 11-05-2001, 12:15 PM