Home > C# > C# String to Int

C# String to Int in windows console Application

In C# a string type is a sequence of Unicode characters. C# string is an alias for String in .NET. In general, we can say that a string is a sequence of characters. C# string to int conversion is not a difficult task. There are a number of String conversion functions available in C# convert class.

C# String to int using Convert Class

We can use any of the Convert.ToInt16 (), Convert.ToInt32(), Convert.ToInt64() to convert a given string to int data type. Given below is a simple example to show how we can do this.

using System;
namespace string_conversion
{
    class Program
    {
         static void Main(string[] args)
         {
              string n;
              short n16 = 0;
              int n32 = 0;
              long n64 =0;
              try
              {
                    Console.WriteLine("Please Enter a Value :");
                    n = Console.ReadLine();
                    n16 = Convert.ToInt16(n);
                    n32 = Convert.ToInt32(n);
                    n64 = Convert.ToInt64(n);

                    Console.WriteLine("Converted to 16 bit Integer : " +
                     n16 + " & Its size is:" +
                    System.Runtime.InteropServices.Marshal.SizeOf(n16) + " bytes");

                    Console.WriteLine("Converted to 32 bit Integer : " + 
                    n32 + " & Its size is:" +
                    System.Runtime.InteropServices.Marshal.SizeOf(n32) + " bytes");

                    Console.WriteLine("Converted to 64 bit Integer : " + 
                    n64 + " & Its size is:" +
                    System.Runtime.InteropServices.Marshal.SizeOf(n64) + " bytes");
              }
              catch (FormatException e)
              {
                    Console.WriteLine(e.Message);

              }
         }
    }
}

As we take input from the user and store it in string type variable n. Since ReadLine() function return a string. Then we use different functions of the string class to convert the input to integers. These functions from convert class can only convert strings which contains numeric values. i.e. You can use these functions to convert strings like “123”.

using System;
namespace string_conversion
{
    class Program
    {
         static void Main(string[] args)
         {
              string n = "123";
              short n16 = 0;
              int n32 = 0;
              long n64 =0;
              try
              {
                   n16 = Convert.ToInt16(n);
                   n32 = Convert.ToInt32(n);
                   n64 = Convert.ToInt64(n);

                   Console.WriteLine("Converted to 16 bit Integer : " + 
                   n16 + " & Its size is:" +
                   System.Runtime.InteropServices.Marshal.SizeOf(n16) + " bytes");

                   Console.WriteLine("Converted to 32 bit Integer : " + 
                   n32 + " & Its size is:" +
                   System.Runtime.InteropServices.Marshal.SizeOf(n32) + " bytes");

                   Console.WriteLine("Converted to 64 bit Integer : " + 
                    n64 + " & Its size is:" +
                   System.Runtime.InteropServices.Marshal.SizeOf(n64) + " bytes");
              }
              catch (FormatException e)
              {
                   Console.WriteLine(e.Message);
              }
         }
    }
}

Though if you pass a string like “abc” to these functions your program will generate the exception or following message on the console screen. Input string was not in a correct format. This error occurs because the string “abc” doesn’t adhere to the format of an integer.

C# String to int using Parse and TryParse Functions

 We have other options as well to convert the C# string to int data type. We can do so by using Parse or TryParse functions.

Below examples shows the working of Parse and TryParse functions.

using System;
namespace string_conversion
{
    class Program
    {
         static void Main(string[] args)
         {
              string n = "123";
              short n16 = 0;
              int n32 = 0;
              long n64 =0;
              try
              {
                   n16 = Int16.Parse(n);
                   n32 = Int32.Parse(n);
                   n64 = Int64.Parse(n);

                   Console.WriteLine("Converted to 16 bit Integer : " + 
                   n16 + " & Its size is:" +
                   System.Runtime.InteropServices.Marshal.SizeOf(n16) + " bytes");

                   Console.WriteLine("Converted to 32 bit Integer : " + 
                   n32 + " & Its size is:" +
                   System.Runtime.InteropServices.Marshal.SizeOf(n32) + " bytes");

                   Console.WriteLine("Converted to 64 bit Integer : " + 
                    n64 + " & Its size is:" +
                   System.Runtime.InteropServices.Marshal.SizeOf(n64) + " bytes");
              }
              catch (FormatException e)
              {
                   Console.WriteLine(e.Message);
              }
         }
    }
}

Taking input from the user.

using System;
namespace string_conversion
{
    class Program
    {
         static void Main(string[] args)
         {
              string n ;
              short n16 = 0;
              int n32 = 0;
              long n64 =0;
              try
              {
                   Console.WriteLine("Please Enter a value : ");
                   n = Console.ReadLine();
                   n16 = Int16.Parse(n);
                   n32 = Int32.Parse(n);
                   n64 = Int64.Parse(n);

                   Console.WriteLine("Converted to 16 bit Integer : " + 
                   n16 + " & Its size is:" +
                   System.Runtime.InteropServices.Marshal.SizeOf(n16) + " bytes");

                   Console.WriteLine("Converted to 32 bit Integer : " + 
                   n32 + " & Its size is:" +
                   System.Runtime.InteropServices.Marshal.SizeOf(n32) + " bytes");

                   Console.WriteLine("Converted to 64 bit Integer : " + 
                    n64 + " & Its size is:" +
                   System.Runtime.InteropServices.Marshal.SizeOf(n64) + " bytes");
              }
              catch (FormatException e)
              {
                   Console.WriteLine(e.Message);
              }
         }
    }
}

C# string to int

 

 

 

If we give a string like abc we get an error on the console screen.

C# convert string to int error

 

 

 

Using the TryParse function to convert the input string.

We can use TryParse Function as:

     Int16.TryParse(n, out n16);
     Int32.TryParse(n, out n32);
     Int64.TryParse(n, out n64);  

n16, n32, and n64 are the output variables in which TryParse function will save values after conversion and out points it in the above statements.

C# Convert String to Int in windows form applications

Just like console applications, we may also need to convert a C# string to Int in windows form applications. In windows form applications anything we take input in a text box or text area is a string. We can use the above-mentioned functions to do the same task. i.e. convert a c# string to int using all the above-mentioned functions.

For example, if we have a text box named as “TextBox1” and we need to convert the input of this text box to integer data type. We can do this as:

try{
//Using Convert Class Functions
     n16 = Convert.ToInt16(TextBox1.Text);
     n32 = Convert.ToInt32(TextBox1.Text);
     n64 = Convert.ToInt64(TextBox1.Text);

// using Parse
     n16 = Int16.Parse(TextBox1.Text);
     n32 = Int32.Parse(TextBox1.Text);
     n64 = Int64.Parse(TextBox1.Text);  

// using TryParse
     Int16.TryParse(TextBox1.Text, out n16);
     Int32.TryParse(TextBox1.Text, out n32);
     Int64.TryParse(TextBox1.Text, out n64);  
}
catch (FormatException e)
{
// Exception handling code
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.