• 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

  • Anonymous Types in C#
    Hi friends! Today we are going to learn about Anonymous Types in C#. Let's start with the Introduction Anonymous is a type that does...
  • Web API Attribute Routing Route Constraints
    In this article, I will discuss the Web API Attribute Routing Route Constraints with examples. We are going to work with the same example ...
  • 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...
  • What is Web API in Asp.Net
    In this article, I will be introducing you to ASP.NET WEB API Framework. At the end of this article, you will be having a very good underst...
  • Usability of SecureString object in C#
    Introduction Hi friends! In this blog we will be discussing a very interesting as well as useful topic in C# and that is Securestring objec...
  • Reverse Number Program in C# with Examples
    In this article, I am going to discuss the Reverse Number Program in C# with some examples. Please read our previous article where we discu...
  • Connecting to SQL Server using SSMS
    Introduction Hi friends! In this blog we will be discussing How to connect to the SQL Server using SQL Server Management Studio (SSMS). ...

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