Binary to Real

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

Program code:

/*

* Created by SharpDevelop.

* User: Peric Zeljko

* Date: 7.10.2011

* Time: 8:51

*

*

* Simple console program for conversion of

* Binary numbers to Real numbers

*

*/

using System;

namespace Binary_to_Decimal

{

class Program

{

public static void Main(string[] args)

{

//

// declaration of variables

//

string [] DecimalFloat = new string[2];

string Binary = "";

bool RealRelevance = false;

bool BinaryRelevance = false;

int Index = 0;

int Lenght = 0;

int Exponent = 0;

int Cipher = 0;

double BaseExponent = 0;

double RealNumber = 0;

double D = 0;

double F = 0;

string Negative = "";

string DecimalPart = "";

string FloatPart = "";

string Letter = "";

//

// not very desirable but necessary

// begining of program label

//

begining :

//

// Hello message to user

//

Console.Title = "Program Binary to Real ";

Console.SetWindowSize(80,40);

Console.ForegroundColor = ConsoleColor.Green;

Console.Clear();

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

Console.WriteLine(" Binary to Real");

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

//

// Get number from Console

//

Console.WriteLine();

Console.Write("Enter binary number (exmpl: 10,011 = 2,375) : ");

Binary = "";

Binary = Console.ReadLine();

Console.WriteLine();

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

//

// Check if it is Real number

//

RealRelevance = false;

RealNumber = 0;

RealRelevance = double.TryParse(Binary,out RealNumber);

if (RealRelevance == true)

{

//

// clear sign plus or minus

//

if (Binary.Contains("-"))

{

Binary = Binary.TrimStart('-');

Negative = "- ";

RealNumber = -1 * RealNumber;

}

else if (!Binary.Contains("-") && !Binary.Contains("+"))

{

Negative = "";

}

else if (Binary.Contains("+"))

{

Binary = Binary.TrimStart('+');

Negative = "+ ";

RealNumber = double.Parse(Binary.ToString());

}

//

// Clears number from 0 if it is number with float point

// with all possibilites

//

// exmpl. 0001101,01010000 = 1101,0101

//

if ( Binary.Contains(","))

{

DecimalFloat = Binary.Split(',');

DecimalPart = DecimalFloat[0];

FloatPart = DecimalFloat[1];

DecimalPart = DecimalPart.TrimStart('0');

if (DecimalPart =="")

{

DecimalPart = "0";

}

FloatPart = FloatPart.TrimEnd('0');

if (FloatPart =="")

{

FloatPart = "0";

}

if (DecimalPart =="0" && FloatPart =="0")

{

Binary = "0";

}

if (DecimalPart !="0" && FloatPart =="0")

{

Binary = DecimalPart;

}

if (DecimalPart =="0" && FloatPart !="0")

{

Binary = DecimalPart + "," + FloatPart;

}

if (DecimalPart !="0" && FloatPart !="0")

{

Binary = DecimalPart + "," + FloatPart;

}

}

//

// if it is decimal number

//

else

{

Binary = Binary.TrimStart('0');

if (Binary =="")

{

Binary = "0";

}

}

}

else if (RealRelevance == 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" && Letter !=",")

{

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 Float number, split number to Decimal and Float part

// exmpl. 11.011(2) DecimalPart = 11 and FloatPart = 011

//

if (Binary.Contains(","))

{

DecimalFloat = Binary.Split(',');

DecimalPart = DecimalFloat[0];

FloatPart = DecimalFloat[1];

//

// Decimal part first

//

Exponent = 0;

BaseExponent = 0;

Cipher = 0;

D = 0;

Index = 0;

Lenght = 0;

Letter = "";

Lenght = DecimalPart.Length;

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

{

Exponent = Lenght - Index - 1;

Letter = DecimalPart.Substring(Index,1);

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

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

D = D + (Cipher * BaseExponent);

}

//

// Float part second

//

Exponent = 0;

BaseExponent = 0;

Cipher = 0;

F = 0;

Index = 0;

Lenght = 0;

Letter = "";

Lenght = FloatPart.Length;

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

{

Exponent = Index+1;

Letter = FloatPart.Substring(Index,1);

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

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

F = F + (Cipher * BaseExponent);

}

RealNumber = D + F;

//

// goto jump is not very desirable but necessary

//

//

goto write;

}

//

// Resolving conversion from Binary to Decimal

// If it is Decimal number

// exmpl. 1001

//

else

{

Exponent = 0;

Cipher = 0;

RealNumber = 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());

RealNumber = RealNumber + (Cipher * BaseExponent);

}

}

//

// not very desirable but necessary

// write label

//

write:

//

// Write result

//

if (RealNumber == 0)

{

Negative ="";

}

Console.WriteLine();

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

Console.WriteLine();

Console.WriteLine("Real number : " + Negative + RealNumber.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;

}

}

}

}

All the best,

Author