Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
How to Convert an Integer to a String in C?
Next article icon

C# Program to Convert a Binary String to an Integer

Last Updated : 02 Jul, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report

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




// 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);
    }
}
 
 

Output:

Number value of binary "1010101010101010" is = 43690  

Program 2: 
 

C#




// 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();
    }
}
}
 
 

Output:
 

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


Next Article
How to Convert an Integer to a String in C?

S

shivanisinghss2110
Improve
Article Tags :
  • C#
  • C# Programs

Similar Reads

  • C# Program for Converting Hexadecimal String to Integer
    Given an hexadecimal number as input, we need to write a program to convert the given hexadecimal number into equivalent integer. To convert an hexadecimal string to integer, we have to use Convert.ToInt32() function to convert the values. Syntax: Convert.ToInt32(input_string, Input_base); Here, inp
    2 min read
  • How to Convert an Integer to a String in C?
    Write a C program to convert the given integer value to string. Examples Input: 1234Output: "1234"Explanation: The integer 1234 is converted to the string "1234". Input: -567Output: "-567"Explanation: The integer -567 is converted to the string "-567". Different Methods to Convert an Integer to a St
    4 min read
  • C/C++ Program to Count set bits in an integer
    Write an efficient program to count number of 1s in binary representation of an integer. Examples : Input : n = 6 Output : 2 Binary representation of 6 is 110 and has 2 set bits Input : n = 13 Output : 3 Binary representation of 11 is 1101 and has 3 set bits Recommended: Please solve it on “PRACTICE
    2 min read
  • C Program to Add 2 Binary Strings
    Given two Binary Strings, we have to return their sum in binary form. Approach: We will start from the last of both strings and add it according to binary addition, if we get any carry we will add it to the next digit. Input: 11 + 11Output: 110[GFGTABS] C // C Program to Add 2 Binary Strings // and
    8 min read
  • C Program For Boolean to String Conversion
    To convert boolean to string in C we will use the following 2 approaches: Using Conditional StatementsUsing Ternary Operator Input: bool n = true Output: string true1. Using Conditional Statements C/C++ Code // C program to demonstrate Boolean to String // Conversion using conditional statements #in
    1 min read
  • C# Program to Find Binary Equivalent of an Integer using Recursion
    Given an integer number, now we convert the given integer number into a binary number using recursion. Recursion is a method in which a function calls itself directly or indirectly and such type of function is known as a recursive function. It solves the problem very efficiently like we find the bin
    3 min read
  • C program to count zeros and ones in binary representation of a number
    Given a number N, the task is to write C program to count the number of 0s and 1s in the binary representation of N. Examples: Input: N = 5 Output: Count of 0s: 1 Count of 1s: 2 Explanation: Binary representation of 5 is "101".Input: N = 22 Output: Count of 0s: 2 Count of 1s: 3 Explanation: Binary r
    5 min read
  • C Program For Long to String Conversion
    To convert the long to string in C language we will use the following 2 approaches: Using Macros with sprintfUsing sprintf Input: long = 1234 Output: string 12341. Using Macros and sprintf C/C++ Code // C Program for Long to // String Conversion #include <stdio.h> #include <string.h> #de
    1 min read
  • C program to read a range of bytes from file and print it to console
    Given a file F, the task is to write C program to print any range of bytes from the given file and print it to a console. Functions Used: fopen(): Creation of a new file. The file is opened with attributes as “a” or “a+” or “w” or “w++”.fgetc(): Reading the characters from the file.fclose(): For clo
    2 min read
  • C program to set K-th bit of a number N
    Given a number N and an integer K, the task is to set the Kth bit of the number N, i.e., if the Kth bit is 0, then set it to 1 and if it is 1 then leave it unchanged. Examples: Input: N = 5, K = 2Output: 7Explanation: 5 is represented as 101 in binary and has its second bit 0, so setting it will res
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences