تبدیل اعداد باینری به دسیمال

تبدیل اعداد باینری به دسیمال راحت است. فرض کنید عدد باینری به دست آمده در درس قبل را می خواهیم به معادل دسیمال آن تبدیل کنیم :

binary-to-decimal-conversion-01
برای این کار کافیست تمام اعدادی را که عدد 1 بر روی آنها قرار دارد را با هم جمع کنیم:

binary-to-decimal-conversion-02

حال اگر بخواهید، مراحل بالا را با استفاده از کدنویسی انجام دهید، باید به صورت زیر عمل کنید :

using System;

namespace BinaryToDecimal
{
    class Program
    {
        static void Main(string[] args)
        {
            int number, binaryValue, decimalValue = 0, baseValue = 1, remainder;
            Console.Write("Enter a Binary Number(1s and 0s) : ");
            number = int.Parse(Console.ReadLine());
            binaryValue = number;
            while (number > 0)
            {
                remainder = number % 10;
                decimalValue = decimalValue + remainder * baseValue;
                number = number / 10;
                baseValue = baseValue * 2;
            }
            Console.Write("The Binary Number is : " + binaryValue);
            Console.Write("\nIts Decimal Equivalent is : " + decimalValue);
            Console.ReadLine();
        }
    }
}
Enter a Binary Number(1s and 0s) : 110110
The Binary Number is : 110110
Its Decimal Equivalent is : 54