• Home
  • About
  • Contact
  • ado.net
  • angular
  • c#.net
  • design patterns
  • linq
  • mvc
  • .net core
    • .Net Core MVC
    • Blazor Tutorials
  • sql
  • web api
  • dotnet
    • SOLID Principles
    • Entity Framework
    • C#.NET Programs and Algorithms
  • Others
    • C# Interview Questions
    • SQL Server Questions
    • ASP.NET Questions
    • MVC Questions
    • Web API Questions
    • .Net Core Questions
    • Data Structures and Algorithms

Tuesday, August 11, 2020

Binary to Decimal Conversion in C# with Examples

 Admin     August 11, 2020     C#, Interview Question, Programming, Programs     No comments   

In this article, I am going to discuss the Binary to Decimal Conversion in C# with some examples. Please read our previous article where we discussed the Decimal to Binary Conversion Program in C# with examples. In C#, we can easily convert a binary number (base-2 (i.e. 0 or 1)) into decimal number (base-10 (i.e. 0 to 9)).

How to Convert Binary Number to Decimal Number in C#?
The following diagram shows how to convert a binary number i.e. 10101 into its equivalent decimal number i.e. 21.
Binary to Decimal Conversion in C#

Implementing Binary to Decimal Conversion Program in C#:
In the following example, first, we extract the digits of a given binary number starting from rightmost digits using the modulus operator. Once we extract the number, then we multiply the digit with the proper base i.e. the power of 2 and add the result into the decimal value variable. In the end, the decimal value variable will hole the required decimal number.
using System;
namespace LINQDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter the Binary Number : ");
            int binaryNumber = int.Parse(Console.ReadLine());
            int decimalValue = 0;
            // initializing base1 value to 1, i.e 2^0 
            int base1 = 1;
            
            while (binaryNumber > 0)
            {
                int reminder = binaryNumber % 10;
                binaryNumber = binaryNumber / 10;
                decimalValue += reminder * base1;
                base1 = base1 * 2;
            }
            Console.Write($"Decimal Value : {decimalValue} ");
            Console.ReadKey();
        }
    }
}
Output:
Implementing Binary to Decimal Conversion in C#

Please Note: The above program works with binary numbers within the range of integers. If you want to work with long binary numbers such as 20 bits or 30 bit, then you need to use the string variable to store the binary numbers.

Binary to Decimal Conversion using Convert.ToInt32() method:
In the following example, we are using the ToInt32 method to convert a binary number to a decimal number. This except expects two parameters. The first parameter is the string representation of the binary number and the second parameter is the base value i.e. in our case it is 2.
using System;
namespace LINQDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter the Binary Number : ");
            int binaryNumber = int.Parse(Console.ReadLine());
            int decimalValue = Convert.ToInt32(binaryNumber.ToString(), 2);           
            Console.Write($"Decimal Value : {decimalValue} ");
            Console.ReadKey();
        }
    }
}
Output:
Binary to Decimal Conversion using Convert.ToInt32() method

In the next article, I am going to discuss the Character Occurrences in a String Program in C# with some examples. Here, in this article, I try to explain the Binary to Decimal Conversion Program in C# with some examples.

Summary:
I hope this post will be helpful to understand Binary to Decimal Conversion in C#
Please share this post with your friends and colleagues.
For any queries please post a comment below.
Happy Coding 😉
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Post Older Post

0 comments:

Post a Comment

If you like this website, please share with your friends on Facebook, Twitter, LinkedIn.

Join us on Telegram

Loved Our Blog Posts? Subscribe To Get Updates Directly To Your Inbox

Like us on Facebook

Popular Posts

  • Creating and Working with Database
    Hi friends! In our last post we have seen different approaches how we can connect with SQL Server Management Studio(SSMS). Today, We are g...
  • Entity Types in Entity Framework
    In this article, I am going to discuss the Entity Types in Entity Framework in detail. Please read our previous article where we discussed...
  • Introduction to Entity Framework
    Before .NET 3.5 as a developer, we often used to write ADO.NET code to perform CRUD operation with the underlying database. For this, we ne...
  • Filters in ASP.Net MVC
    Hi friends! Today we are going to discuss about using a very important feature of MVC i.e. “Filters“ . Filters are a unique feature of Asp...
  • Data Parallelism in C#
    Introduction: Hi, in this blog we are going to discuss a very important feature of C# that is data parallelism. Data parallelism means the ...
  • C# Programming Examples on Sorting
    Today i am going to tell you some of the Sorting programming questions in C#. Q1- Write a C# program to perform Selection sort. Ans:  Sel...
  • ASP.NET Web API Basic Authentication
    In this article, I am going to discuss how to implement the ASP.NET Web API Basic Authentication step by step with an example. Please read...

Blog Archive

Contact Form

Name

Email *

Message *

Tags

.Net .Net Core .Net Core MVC Algorithm Angular Anonymous Types Asp.Net Asp.Net MVC Blazor C# Data Structure Database Design Patterns Entity Framework Entity Framework Core Filters Interview Question Management Studio Programming Programs SQL Server SSMS Web API

Copyright © C# Techtics | All Right Reserved.

Protected by Copyscape