Converting a string to int

I'am building a console program and need to convert a string that the user types to an int. If I use convert.ToInt32 a get the ascii code for the number but not the number it self.

can anyone help me find a easy way to do this, if there is one :)



Answer this question

Converting a string to int

  • dagjo

    can't edit my post due to the forums being down unfortunately.....

    the code above posted goes through each character and checks to see if its a Digit, as your code has, and of course will fail when you get a non digit character like - (minus) for example.

    you could do in your code Char.IsDigit(str(i)) but no idea if this would make a difference. My apologies if this does not work



  • Elham Sarikhani

    hmm... tried this

    int theResult = 0;

    if (int.TryParse(StringInput, out theResult)

    {

    //successfully converting the string into an integer and result stored in "theResult"

    }

    does this work for you

    how are you reading the inputs from the console This should work also, but best practice would be the code above since it tries to parse the input string into an integer and doesn't throw an exception unlike the way it does when you just give a string that contains letters in the Convert.ToIn32() method.

    Console.Write("Enter: ");

    string input = Console.ReadLine();

    int theResult = 0;

    if (int.TryParse(input, out theResult))

    {

    Console.WriteLine("input string successfully parsed to Int: " + theResult.ToString());

    }



  • MarkDu

    interesting. I just tried this and seems to be ok....

    foreach (Char currentChar in StringInput)

    {

    if (Char.IsDigit(currentChar))

    {

    int result = Convert.ToInt32(currentChar);

    Console.WriteLine(result.ToString());

    }

    }

    does this work for you



  • kumarvk

    Hi, Arnar.

    Convert.ToInt32(string) should work, but it would be helpful to see an example of how it is not working as you expect. You can also use int.Parse(string) or int.TryParse(string, out int).

    Note that there is a small difference between Convert.ToInt32(string) and int.Parse(string). If the argument is null, Convert.ToInt32(string) will return 0, while int.Parse(string) will throw a System.ArgumentNullException exception.

    Also, int.TryParse(string, out int) is only available in the .NET Framework 2.0.

    Can you please post an example of how Convert.ToInt32(string) is not working as you expect

    Hope that helps,

    Damon
    Microsoft Visual C#


  • jordan V

    I fixed the problem by doing it this way

    for (int i = 0; i < str1.Length; i++)

    {

    if (Char.IsDigit(str1,i))

    {

    number = Convert.ToString(str1Idea);

    if (int.TryParse(number, out theResult))

    {

    Console.WriteLine("input string successfully parsed to Int: " + theResult.ToString());

    }

    }

    }

    Thanks agin for your help Damon



  • Furrukh Baig

    hi Damon

    the input for my program can be something like this 1-9 7-9

    So my problem is that I need to find just the charcters that are actual numbers and use the in my program

    the code I use to do this looks like this:

    for (int i = 0; i < str1.Length; i++)

    {

    if (Char.IsDigit(str1,i))

    {

    int a = Convert.ToInt32(str1Idea);

    Console.WriteLine(a);

    }

    }

    put this code returns 57 if the number 9 is in the string.

    I use Console.ReadLine()

    to read user input.

    your code workes fine if I only insert numbers but if there is something eles in the string like "-" the program does not work.

    do you see what I am doing wrong in this code

    Thanks for your help so far :)



  • Converting a string to int