Introduction
This code snippet shows how to Convert string representation of binary number to int or Vice Versa in C#
Code
using System;
public class Program
{
public static void Main()
{
int toBase = 2; // Must add base value.
int intvalue = 25;
string binary = Convert.ToString(intvalue, toBase); // int to binary conversion
int result = Convert.ToInt32(binary.Replace(" ", ""), 2); // binary to int conversion
Console.WriteLine("Binary Value is "+ binary);
Console.WriteLine("Binary to Integer Conversion Value is "+ result);
}
}
OutputBinary Value is 11001
Binary to Integer Conversion Value is 25
Binary to Integer Conversion Value is 25
Demo : https://dotnetfiddle.net/KLsn8B
