18+ C# Binary to Decimal Converter Examples

This post contains a total of 18+ Hand-Picked C# Binary to Decimal Converters. All the Binary to Decimal Converters are made using C Sharp Programming Language.

You can use the source codes of these programs for your own personal or educational use with credits to the original owner.

Related Posts

Click a Code to Copy it.

1. By Asgar Ali

Made by Asgar Ali. A simple C# program to convert binary number to decimal. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/* This program converts a valid binary string to decimal and hexadecimal */

namespace SoloLearn
{
    class Program
    {
        static void Main(string[] args)
        {
            string bin = Console.ReadLine();
            var c = bin.ToCharArray();
            Array.Reverse(c);
            double result = 0;
            for(int i = 0;i<= c.GetUpperBound(0);i++)
            {
                result += (int.Parse(c[i].ToString()) * Math.Pow(2,i));
            }
            Console.WriteLine("Binary:  "+bin);
            Console.WriteLine("Decimal: "+result.ToString());
      Int64 x = Convert.ToInt64(result);      Console.WriteLine("Hexadecimal:   "+x.ToString("X"));
        }
    }
}

2. By Caroline Popova

Made by Caroline Popova. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SoloLearn
{
    class Program
    {
        static void Main(string[] args)
        {//ΠΏΠ΅Ρ€Π΅Π²ΠΎΠ΄ чисСл ΠΈΠ· Π΄Π²ΠΎΠΈΡ‡Π½ΠΎΠΉ систСмы счислСния (0 ΠΈ 1) Π² Π΄Π΅ΡΡΡ‚ΠΈΡ‡Π½ΡƒΡŽ (0-9)
            string bin = Console.ReadLine();
            Console.Write(bin+" (2) = ");
            int n = bin.Length;
            double S = 0;
            int cyf;
            double deg = 0;
            double sumNum;
            int i, j = 0;

            for (i = n-1;i >= 0;i--)
            {
                cyf = (int)bin[i]-'0';
                if (cyf == 0 || cyf == 1)
                {
                 deg = Math.Pow(2, j);
                 sumNum = cyf * deg;
                 S += sumNum;
                 j++;
                }
                else 
                {
                   Console.WriteLine(" Error"); 
                   Console.WriteLine(); Console.WriteLine("Please, enter a binary number consisting of 1 and 0");
                    S = -1;
                    break;
                }
            }
            if (S >= 0)
            {
            Console.WriteLine(S + " (10)");
            }
        }
    }
}

3. By omar khalid alnashar

Made by omar khalid alnashar. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SoloLearn
{
    class Program
    {
    
        public static string reverseString(string t)
        {
            char[] array = t.ToCharArray();
            Array.Reverse(array);
            return new String(array);
        }
        static int convertToDec(string n)
        {
           int add = 1;
           int result = 0;
           
           foreach (char e in reverseString(n))
           {
              if (e == '1') 
              result  = result + add;
              
              add = add * 2;
           }
           return result;
        }
        static void Main(string[] args)
        {
            string binary = Console.ReadLine();
            Console.WriteLine(convertToDec(binary));
        }
    }
}

4. By Malek Alsset

Made by Malek Alsset. ( Source )

class MA {

    public static int binaryToDecimal(int n)

    {

        int num = n;

        int dec_value = 0;
 

        // Initializing base1

        // value to 1, i.e 2^0

        int base1 = 1;
 

        int temp = num;

        while (temp > 0) {

            int last_digit = temp % 10;

            temp = temp / 10;
 

            dec_value += last_digit * base1;
 

            base1 = base1 * 2;

        }
 

        return dec_value;

    }
 

    public static void Main()

    {

        int num = 10101001;
 

        System.Console.Write(binaryToDecimal(num));

    }
}

5. By Chuzzy

Made by Chuzzy. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/* A binary number is made up of elements called bits where each bit can be 1 or 0 */
namespace SoloLearn
{
    class Program
    {
        static string toBinary(int n)
        {
            if (n == 0)
            {
                return "0";
            }
    
            string binary = "";
            while (n > 0)
            {
                // The % (modulus) operator returns the remainder of the division
                int rem = n % 2;
                // add the result (0 or 1)
                binary = rem + binary;
                // divide the number by 2
                n = n / 2;
            }
    
            return binary;
        }
        
        static int toDenary(string binary)
        {
           int denary = 0;
           for (int i = binary.Length; i > 0; i--) 
           {
           
              if (binary[binary.Length - i] == '1') 
              {
                 denary += (int)Math.Pow(2, i - 1);
              }
           }
           return denary;
        }
        
        static void Main(string[] args)
        { 
            //sample number
            int number = 156;
            string binary = "10011100";
            Console.WriteLine(toBinary(number));
            Console.WriteLine(toDenary(binary));
        }
    }
}

6. By Sanjeev Kumar

Made by Sanjeev Kumar. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Binary_Decimal
{
    class Program
    {
        static void Main(string[] args)
        {
/* Kindly run the Program and input a binary number.The codes will convert it to decimal number.
you can also input a decimal number. The codes will return you the same number.*/

 double n=Convert.ToDouble(Console.ReadLine());
         double num=n;
         int i=0; 
          
         while(num>0)
         {
         num=(num-num%10)/10;
         i++;
         }//Counts the digits in input number
         
         num=n;
         int j=0;
         double b_decimal=0;
         while(num>0)
         {
         if(num%10 != 1 && num%10 != 0)
         break;
         b_decimal += (num%10)*Math.Pow(2,j);
          num=(num-num%10)/10;
          j++;
          }//Converts the binary number into decimal number.
                    
          if(i==j)
          
          Console.Write("The decimal conversion of '"+n+"' = "+b_decimal+"\n\n{0:T}  {0:D}\n\nPlz Hit Like and Comment.",DateTime.Now);
          
          else
          
          Console.Write("Your input '"+n+"' is already a decimal number\n\n{0:T}  {0:D}\n\nPlz Hit Like and Comment",DateTime.Now);
         }
     }  
}

 

7. By Piotr Tyc

Made by Piotr Tyc. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SoloLearn
{
    class Program
    {
    // binary to decimal
        static void Main(string[] args)
        {
          string binary = Console.ReadLine();
          char[] bin = binary.ToCharArray();
          
    
          Array.Reverse(bin);
          int dec = 0;
          
          for (int j = 0; j < bin.Length; j++)
          { 
          if (bin[j] == '1')
              {
              dec += (int)Math.Pow(2, j);
              }
          }
        Console.WriteLine(dec);
        }
    }
}

8. By Abdul ArroVi

Made by Abdul ArroVi. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SoloLearn
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = Convert.ToInt32(Console.ReadLine());
            int a = x;
            int b = 0;
            int c = 1;
            while (a > 0) {
            b += ((a%10) * c);
            a /= 10;
            c *= 2;}
            Console.WriteLine("The equivalent of binary number " + x + " in decimal is " + b);
        }
    }
}

9. By ilya.iz

Made by ilya.iz. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


// my C# Converter "Binary-->Decimal"

namespace SoloLearn
{
    class Program
    {   
        static void Decimal(string x)
        {   
          string dec =  Convert.ToInt32(x, 2).ToString(); 
            Console.WriteLine("Decimal-Result: "+dec);
        }
        
        static void Main(string[] args)
        {  
           string b = Console.ReadLine();
           Console.WriteLine("Your Binary-number is: "+b);
           Decimal(b); 
        }
    }
}

10. By Nathan Breen

Made by Nathan Breen. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SoloLearn
{
    class Program
    {
        static void Main(string[] args)
        {
            ToDecimal(Console.ReadLine());
        }
        
        static void ToDecimal(string b)
        {
            int sum = 0;
            int degree = b.Length - 1;
            
            for(int i = 0; i < b.Length - 1; i++)
            {
                if(b[i] == '1')
                    sum += (int)Math.Pow(2, degree);
                    degree--;
            }
            Console.WriteLine(sum);
        }
    }
}

11. By Vanty Artyom Dmitrievich

Made by Vanty Artyom Dmitrievich. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SoloLearn
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = 11100100;
                double result = 0;
                for (int i = 0; i < num.ToString().Length; i++)
                {
                    result += (int.Parse(Convert.ToString(num.ToString()[i])) * (Math.Pow(2, (num.ToString().Length - 1) - i)));
                }
                Console.WriteLine(result);
        }
    }
}

12. By Matteo Brancatelli

Made by Matteo Brancatelli. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SoloLearn
{
    class Program
    {
        static void Main(string[] args)
        {
        double sum = 0;
         Console.WriteLine("Input your binary number to convert:");
         int n = Convert.ToInt32(Console.ReadLine());
         
        int strn = n.ToString().Length;
        
        for (int i = 0; i < strn; i++) { 
        
        int lastDigit = n % 10; 
        
        sum = sum + lastDigit * (Math.Pow(2, i));
        n = n / 10; 
         } 
         Console.WriteLine("Answer: " + sum);            
        }
    }
}

13. By Sunny Bejugam

Made by Sunny Bejugam. ( Source )

using System;
using Xunit;
using BinaryStringToDecimal;


namespace BinaryStringToDecimalTest1
{
  public class UnitTest1
  {
    [Fact]
    public void ParseBinary_Check_Execution_ReturnDecimal()
    {
      //arrange
      var obj = new Program();
      //act
      var result = obj.ParseBinary("00001111");

      // assert
      Assert.Equal(15, result);

    }
  }
}

14. By Dvalmont07

Made by Dvalmont07. ( Source )

using BinaryToDecimal_Application;
using System;

namespace BinaryToDecimal_Console
{
    class Program
    {
        private static void Main(string[] args)
        {
            if (args is null)
            {
                throw new ArgumentNullException(nameof(args));
            }

            try
            {
                Console.WriteLine("Type a binary sequence");
                Convertion convertion = new Convertion();
                string binarySequence = Console.ReadLine();

                Console.WriteLine($" The decimal value of {binarySequence} is {convertion.ConvertBinaryToDecimal(binarySequence)}");
                Console.WriteLine($" The decimal value of using a simplified method is {convertion.ConvertBinaryToDecimalSimplefied(binarySequence)}");

                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

15. By AdmiralReality

Made by AdmiralReality. ( Source )

using BinaryToDecimalConverter;

DecimalConverter converter = new();
if (args.Length == 0)
{
    GreetRealtime();

    string? input;
    do
    {
        input = Console.ReadLine();
        converter.Convert(input);
    }
    while (input?.ToUpper() != "QUIT");
}
else
{
    GreetArgs();

    foreach (var str in args)
    {
        if (converter.TryConvert(str, out var result))
            Console.WriteLine($"{str} => {result}");
        else
            Console.WriteLine($"{str} is invalid");
    }
}

void GreetRealtime()
{
    string text = "Binary to decimal converter application. Input decimal number " +
        "(number, based binary system, containing only \"0\" and \"1\"). Type \"quit\" to exit. " +
        "Application also supports running with \"args\".";
    Console.WriteLine(text);
}

void GreetArgs()
{
    string text = "Binary to decimal converter. Running on values provided with \"args\". " +
        "Application also supports realtime mode if you run it with empty \"args\".\n\n" + 
        "{binary} => {decimal}";
    Console.WriteLine(text);
}

16. By DavidBarth

Made by DavidBarth. ( Source )

/*
 * Binary to Decimal Converter
 * Converts binary to decimal number.
 * v1.0 2016
 * David B.
*/


using System;
using System.Collections;

namespace binaryToDecimal
{
	class MainClass
	{
		public static void calculateDec()
		{
			Console.WriteLine("Enter your binary number: ");
			string number = Convert.ToString(Console.ReadLine());
			int[] numberArray = new int[number.Length];
			for (int i = 0; i < number.Length; i++)
			{
				numberArray[i] = number[i];
			}

			int result = 0;
			int power = 0;

			for (int i = numberArray.Length - 1; i >= 0; i--)
			{
				int placeholder = numberArray[i] - 48;
				if (placeholder == 0)
				{
					result = result;
				}
				else
				{
					result = result + (int)Math.Pow(2, power);
				}

				power = power + 1;

			}

			Console.WriteLine(result);

		}

		public static void Main(string[] args)
		{
			calculateDec();
		}
	}
}

17. By santafiora

Made by santafiora. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DecimalToBinaryWithModulo
{
    class Program
    {      /// <summary>
           /// Doxygen Doku bzw. XML Format Doku
           /// Die Parameter aus der Main habe keine Bedeutung!
           /// </summary>
           /// <param name="args"></param>
        static void Main(string[] args)
        {

            int dezimalZahl;
            Console.WriteLine("Geben Sie eine Dezimalzahl ein:");


            dezimalZahl = Convert.ToInt32(Console.ReadLine());
            string ergebnisBinary = string.Empty;



            for (int i = 0; dezimalZahl > 0; i++)
            {
                ergebnisBinary = dezimalZahl % 2 + ergebnisBinary;
                dezimalZahl /= 2;
            }

            Console.WriteLine($"Die BinΓ€re Zahl lautet: {ergebnisBinary}");
            Console.ReadKey();

        }
    }
}

18. By FarahWheelbarrah

Made by FarahWheelbarrah. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Binary_to_Decimal___Decimal_to_Binary
{
    class Program
    {
        static void Main(string[] args)
        {
            String choice;
            help();
            while ((choice = readString("Choice (d/b/h/x):")) != "x")
                {
                switch (choice)
                {
                    case "b": binarytoDecimal(); break;
                    case "d": decimalToBinary(); break;
                    case "h": help(); break;
                    default: help(); break;
                }
            }
            
        }

        public static String readString(String prompt)
        {
            Console.Write(prompt + " ");
            String input = Console.ReadLine();
            return input;
        }

        public static void binarytoDecimal()
        {
            ulong number;
            String binaryvalue;
            Console.WriteLine("Enter \"back\" to return to the main menu");
            while ((binaryvalue = readString("Enter binary value:")) != "back")
            {
                if (!ulong.TryParse(binaryvalue, out number) || hasOtherNumbers(binaryvalue))
                    Console.WriteLine("ERROR - Invalid input");
                else
                {
                    convertFromBinaryToDecimal(number);
                }
            }



        }

        public static void decimalToBinary()
        {
            ulong number;
            String decimalvalue;
            Console.WriteLine("Enter \"back\" to return to the main menu");
            while ((decimalvalue = readString("Enter decimal value:")) != "back")
            {
                if (!ulong.TryParse(decimalvalue, out number))
                    Console.WriteLine("ERROR - Invalid input");
                else
                {
                    convertFromDecimalToBinary(number);
                }

            }
                

        }

        public static void help()
        {
            Console.WriteLine("--HELP--");
            Console.WriteLine("Enter 'd' to convert from decimal to binary");
            Console.WriteLine("Enter 'b' to convert from binary to decimal");
            Console.WriteLine("Enter 'h' to see this menu again");
            Console.WriteLine("Enter 'x' to exit the program");
            
        }

        public static void convertFromDecimalToBinary(ulong number)
        {
            bool is0 = true;
            int c = (int)Math.Log(number, 2);
            Console.Write("Binary value: ");
            for (int i = c; i >= 0; i--)
            {
                ulong powerof2 = (ulong)Math.Pow(2, i);
                if (number >= powerof2)
                {
                    number -= powerof2;
                    Console.Write(1);
                    is0 = false;

                }
                else
                    Console.Write(0);
            }

            if (is0)
                Console.Write(0);

            Console.WriteLine();
            
            
        }

        public static void convertFromBinaryToDecimal(ulong number)
        {
            ulong sum = 0;
            String stringnumber = "" + number;
            for (int i = 0; i<stringnumber.Length; i++)
            {
                char c = stringnumber[i];
                int x = (int)Char.GetNumericValue(c);
                if (x != 0)
                    sum += (ulong)Math.Pow(2, (stringnumber.Length - 1 - i));
            }
            Console.WriteLine("Decimal value: " + sum);
            sum = 0;
        }

        public static bool hasOtherNumbers(String value)
        {
            String[] othernumbers = { "2", "3", "4", "5", "6", "7", "8", "9" };
            foreach (String number in othernumbers)
            {
                if (value.Contains(number))
                    return true;
            }
            return false;
        }
    }
}

19. By FedericoSlongo

Made by FedericoSlongo. ( Source )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Binary_to_Decimal_Converter
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Inert how many number you wanna convert: ");
            int array_lenght;
            do { } while (int.TryParse(Console.ReadLine(), out array_lenght));
            double[] numeri_binari = new double[array_lenght];
            double numero_convertito = 0;
            for (int i = 0; i < array_lenght; i++)
            {
                Console.Write($"Insert the {i} binary number: ");
                do { } while ((double.TryParse(Console.ReadLine(), out numeri_binari[i]) && !(numeri_binari[i] == 0 || numeri_binari[i] == 1)));
            }
            for (int i = 0; i < array_lenght; i++)
            {
                if (numeri_binari[i] == 0)
                {
                    continue;
                }
                else
                {
                    numero_convertito += Math.Pow(2, i);
                }
            }
            Console.WriteLine($"The converted number is {numero_convertito}");

            Console.ReadKey();
        }
    }
}