Binary to Decimal Conversion and Vice Versa with Illustration and Example

Introduction

Radix is a Latin word for "root". Root can be considered a synonym for base in the arithmetical sense. In mathematical numeral systems, the radix or base is the quantity of one of a kind digits, including zero, used to render to numbers in a positional numeral system. For instance, in the base-10 or decimal number framework (the most well-known framework being used today), there is an aggregate of 10 digits utilized (0,1,2,3,4,5,6,7,8,9), thusly, its radix is 10. In the base-2 or binary number framework (utilized inside by almost all PCs), there are two numbers utilized (0, 1), so its radix is two. Different words that are synonymous with radix are base and root, in a arithmetic sense. 

In any standard positional numeral system, a number is traditionally composed as (x)y with x as the series of digits and y as its base, in spite of the fact that for base ten the subscript is normally expected (and discarded, together with the match of enclosures), as it is the most widely recognized approach to express esteem. For instance, (100)dec or (100)10 = 100 (in the decimal system) speaks to the main hundred, while (100)2 (in the binary framework with base 2) speaks to the number four. In addition to decimal and binary, there are also other numerical systems as shown below

Other Base/Radix
Other Base/Radix
Almost all PCs utilize Base-two or binary numeral system. The two digits are "0" and "1", communicated from turns showing OFF and ON separately. Binary system is utilized as a part of most electric counters. Octal system is also once in a while utilized as a part of registering. The eight digits are "0,1,2,3,4,5,6,7" and render to 3 bits (23). Decimal system is the most utilized system of numbers on the whole world and you generally utilized as a part of day by day. Its ten digits are "0,1,2,3,4,5,6,7,8,9". Most mechanical counters utilized by this system. Duodecimal (dozenal) system is now and again pushed because of detachability by 2, 3, 4 and 6. It was customarily utilized as a feature of amounts communicated in handfuls and nets. Hexadecimal system is frequently utilized as a part of figuring as a compacter portrayal of binary (1 hex digit for every 4 bits). The sixteen digits are "0,1,2,3,4,5,6,7,8,9" trailed by "A,B,C,D,E,F" or "a,b,c,d,e,f". Vigesimal is customary numeral system in a few societies, still utilized by some to count. Sexagesimal system is begun in old Sumer and go to the Babylonians.[2] Used today as the premise of present day roundabout facilitate system (degrees, minutes, and seconds) and time estimating (hours, minutes, and seconds).

Binary to Decimal Conversion

To convert from binary or base 2 to decimal, take a look the image below.
Binary to Decimal Conversion Illustration
Binary to Decimal Conversion Illustration
Each "one" and a "zero" in the binary are multiplied by exponentiation operation where its base is 2 and its base starts from 0 until binary string length minus 1. After that sum every outcome of the process. And here code implementation in C# through illustration above.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BinaryToDecimal
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = Console.ReadLine();
            int sum=0;
            for (int i = input.Length - 1 , j=0; i >= 0;i--,j++ )
            {
                Console.WriteLine(Convert.ToInt32(input[j]) + " * " + Math.Pow(2, i));
                Console.WriteLine((int)Char.GetNumericValue(input[j]) + " * " 
                                       + Math.Pow(2, i));
                sum += ( (int)Char.GetNumericValue(input[j]) * (int) Math.Pow(2,i)  ) ;
            }
            Console.WriteLine(sum);
            Console.ReadLine();
        }
    }
}

To extract an integer value from a char do not use "Convert.ToInt32 ()" because the return of the method is unicode from char of argument. To get the value of corresponding to char then use method "(int) Char.GetNumericValue ()" like the example above.

Decimal to Binary Conversion

To convert from decimal or base 10 into binary or base 2, take a look the image below.
Decimal to Binary Conversion Illustration
Decimal to Binary Conversion Illustration
The decimal value is divided into 2, If the value is divisible by 2 then the value 0 if not 1. Take the division value and repeat the division process. This process takes place until the decimal value is 0. Here the code implementation from the illustration above -
int a = Convert.ToInt32(Console.ReadLine());
            string binery = "";
            int count = 1, max = 1;
            int i = 0;
            while (true)
            {
                binery += (a % 2 != 0) ? 1 : 0;
                a /= 2;
                if (a == 0)
                {
                    break;
                }
                i++;
            }
            char[] resultbinery = binery.ToCharArray();
            Array.Reverse(resultbinery);
            Console.Write("Binery = " );
            foreach(char c in resultbinery)
            {
                Console.Write(c);
            }
            Console.WriteLine();

From the code above, if executed, it will give output like this -

Output of Decimal to Binary
Output of Decimal to Binary



Comments

Popular posts from this blog

Computer Graphics Project "Planet Orbit" Source Code with Glut OpenGL

Quantization of Image Data Implementation in C#

Computer Graphics Project "MINION" Source Code with Glut OpenGL