• 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

  • What is Dependency Injection(DI)
    Hi friends! Today we are going to learn about Dependency Injection and in our last session we have come across Static classes and where it s...
  • 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...
  • Calling Web API Service in a Cross-Domain Using jQuery AJAX
    In this article, I am going to discuss Calling Web API Service in a Cross-Domain Using jQuery AJAX . Please read our previous article befor...
  • ViewBag in ASP.NET Core MVC
    In this article, I am going to discuss the use of ViewBag in ASP.NET Core MVC application with examples. Please read our previous article ...
  • Recursion And Back Tracking
    In this article, I am going to discuss Recursion And BackTracking in detail. Please read our previous article where we discussed Master Th...
  • What is Abstract Class and When we should use Abstract Class
    Hi friends! In our previous sessions we have seen  Difference Between Class and Struct . And in our last session  we learnt Usability of Sec...
  • Binary to Decimal Conversion in C# with Examples
    In this article, I am going to discuss the Binary to Decimal Conversion in C# with some examples. Please read our previous article where w...

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