Binary to Decimal

Given that one of the interesting problem is transferring Binary number to a decimal number, in the appendix you will find a complete solution SharpDevelop IDE written in the C# programming language. Below is just a part of the code that is most important to solve this problem.

Note :

This program solves the problem only in the set of integers.

Program code:

/*

* Created by SharpDevelop.

* User: Peric Zeljko

* Date: 7.10.2011

* Time: 8:51

*

*

* Simple console program for conversion of

* Binary numbers to Decimal

*

*/

using System;

namespace Binary_to_Decimal

{

class Program

{

public static void Main(string[] args)

{

//

// declaration of variables

//

string Binary = "";

bool DecimalRelevance = false;

bool BinaryRelevance = false;

int Index = 0;

int Lenght = 0;

int Exponent = 0;

int Cipher = 0;

double BaseExponent = 0;

double DecimalNumber = 0;

string Letter = "";

//

// not very desirable but necessary

// begining of program label

//

begining :

//

// Hello message to user

//

Console.Title = "Program Binary to Decimal ";

Console.SetWindowSize(80,40);

Console.ForegroundColor = ConsoleColor.Green;

Console.Clear();

Console.WriteLine("****************************************************************************");

Console.WriteLine(" Binary to Decimal");

Console.WriteLine("****************************************************************************");

//

// Get number from Console

//

Console.WriteLine();

Console.Write("Enter binary number (exmpl: 1001) : ");

Binary = "";

Binary = Console.ReadLine();

Console.WriteLine();

Console.WriteLine("****************************************************************************");

//

// Check if it is Decimal number

//

DecimalRelevance = false;

DecimalNumber = 0;

DecimalRelevance = double.TryParse(Binary,out DecimalNumber);

if (DecimalRelevance == true)

{

//

// Clears number from 0

// with all possibilites

//

// exmpl. 0001101 = 1101

//

Binary = Binary.TrimStart('0');

if (Binary =="")

{

Binary = "0";

}

}

else if (DecimalRelevance == false)

{

Console.WriteLine();

Console.WriteLine("Error , you didn't entered a number at all..");

Console.WriteLine();

//

// goto jump is not very desirable but necessary

//

goto end;

}

//

// Check Binary number relevance

//

Index = 0;

Lenght = 0;

Letter = "";

Lenght = Binary.Length;

for (Index=0;Index<Lenght ;Index = Index + 1)

{

Letter = Binary.Substring(Index,1);

if (Letter != "0" && Letter != "1")

{

BinaryRelevance = false;

Index = Lenght;

}

else

{

BinaryRelevance = true;

}

}

if (BinaryRelevance != true)

{

Console.WriteLine();

Console.WriteLine("Error , you didn't enter binary number ...");

Console.WriteLine();

//

// goto jump is not very desirable but necessary

//

goto end;

}

//

// Resolving conversion from Binary to Decimal

// If it is Decimal number

// exmpl. 1001

//

Exponent = 0;

Cipher = 0;

DecimalNumber = 0;

Index = 0;

Lenght = 0;

Letter = "";

Lenght = Binary.Length;

for(Index=0;Index<Lenght ;Index=Index+1)

{

Exponent = Lenght - Index - 1;

Letter = Binary.Substring(Index,1);

BaseExponent = double.Parse(Math.Pow(2,Exponent).ToString());

Cipher = int.Parse(Letter.ToString());

DecimalNumber = DecimalNumber + (Cipher * BaseExponent);

}

//

// Write result

//

Console.WriteLine();

Console.WriteLine("Binary number : " + Binary);

Console.WriteLine();

Console.WriteLine("Decimal number : " + DecimalNumber.ToString());

Console.WriteLine();

//

// not very desirable but necessary

// end of program label

//

end:

Console.WriteLine("****************************************************************************");

Console.WriteLine();

Console.Write("Do you want to run program again y / n : ");

Letter = Console.ReadLine();

if ( Letter == "Y" || Letter == "y")

{

goto begining;

}

}

}

}

After starting the program, the program requires you to enter a binary number say 1001, after entering a number we have a solution 9.

Believe it or not this problem in the context of C# - solves only a single command:

Convert.ToInt32( binary_number , 2 )

But in the the program I have applied a solution that folows the step-by-step conversion of number:

1001 (binary) = 1*2^3 + 0*2^2 + 0*2^1 + 1*2^0 = 9 (decimal)

All the best,

Author