Program to Convert String to Int in C#

This blog defines how to convert string to int using C#.
 
//op=123 
class Program
{
     static void Main(string[] args)
     {
         myprog ob = new myprog();
         int op= ob.str2int("123");
         Console.Write(op);
         Console.ReadLine();
      }
}
 
public int str2int(string ip)  //string to int..
{
     int i = 0, sum = 0;
     while (i < ip.Length)
     {
         if (Convert.ToInt32(ip[i]) < 48 || Convert.ToInt32(ip[i]) > 57)
         {
              Console.WriteLine("Unable to convert it into integer.\n");
               return 0;
           }
            else
            {
                 sum = sum * 10 + (ip[i] - 48);
                  i++;
              }
        }
        return sum;
}
Ebook Download
View all
Learn
View all