C# Program to Convert a Binary String to an Integer

Given an binary string as input, we need to write a program to convert the binary string into equivalent integer. To convert an binary string to integer, we have to use Convert.ToInt32(String, Base/Int32) function to convert the values. The base of the binary is 2. 

Syntax: 

 Convert.ToInt32(String, Base/Int32);

Examples:

Input  : 1010101010101010
Output : 43690

Input : 1100011000
        111100001111
        11001100110011001100

Output : 792
         3855
         838860

Program 1: 
 

csharp

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to convert array
// of binary string to an integer
using System;
using System.Text;
  
class GFG {
  
    static void Main(string[] args)
    {
        // binary number as string
        string bin_strng = "1010101010101010";
        int number = 0;
        
        // converting to integer
        number = Convert.ToInt32(bin_strng, 2);
        
        // to print  the value
        Console.WriteLine("Number value of binary \"{0}\" is = {1}", bin_strng,
                          number);
    }
}

chevron_right


Output:

Number value of binary "1010101010101010" is = 43690

Program 2: 
 

C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to convert array
// of binary string to an integer
using System;
using System.Text;
  
namespace geeks {
class GFG {
    
    static void Main(string[] args)
    {
        // binary number as string
        string bin_strng = "1100011000";
        int number = 0;
  
        // converting to integer
        number = Convert.ToInt32(bin_strng, 2);
        
        // to print  the value
        Console.WriteLine("Number value of binary \"{0}\" is = {1}", bin_strng,
                          number);
  
        bin_strng = "111100001111";
  
        // converting to integer
        number = Convert.ToInt32(bin_strng, 2);
        
        // to print  the value
        Console.WriteLine("Number value of binary \"{0}\" is = {1}", bin_strng,
                          number);
  
        bin_strng = "11001100110011001100";
  
        // converting to integer
        number = Convert.ToInt32(bin_strng, 2);
        
        // to print the value
        Console.WriteLine("Number value of binary \"{0}\" is = {1}", bin_strng,
                          number);
  
        // hit ENTER to exit
        Console.ReadLine();
    }
}
}

chevron_right


Output:
 

Number value of binary "1100011000" is = 792
Number value of binary "111100001111" is = 3855
Number value of binary "11001100110011001100" is = 838860



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.