Sobzirom da je jedan od interesantnih problema prebacivanje binarnog broja u decimalni broj u okviru priloga možete naći kompletno rešenje napisano u IDE SharpDevelop u programskom jeziku C# , i predstavlja konzolnu aplikaciju.Kada preuzmete Binary to Decimal.rar arhivu otvorite je i u okviru nje pronađite Release folder gde je smešten exe fajl.Neophodan .Net framework 4.0 ili noviji.Ukoliko koristite IDE SharpDevelop možete otvoriti Binary to Decimal SharpDevelop Solution i pogledati kompletan kod za ceo program.U nastavku se nalazi samo deo programskog koda koji je najbitniji za rešavanje ovog problema.klik na sliku da preuzmete gotov kompletan program.

Napomena :

Ovaj program rešava problem samo u skupu celih brojeva , u narednom periodu biće urađena verzija koja prebacuje binarne brojeve s pokretnim zarezom u realni broj i obrnuto.Algoritam pokušajte da napravite sami na osnovu dole prikazanog koda.

Kod programa :

/*

* 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;

}

}

}

}

}

Po startovanju programa , program zahteva da unesete binarni broj recimo 1001 , a po unosu dobijate rešenje broj 9.

Verovali ili ne ovaj problem se u okviru C - a rešava i samo jednom komandom :

Convert.ToInt32( binarni_broj,2 )

Međutim u programu je primenjeno rešenje koje upravo pokazuje postupak korak po korak kojim se inače dolazi do pretvaranja broja koji pripada binarnom brojnom sistemu u decimalni brojni sistem.Radi se o postupku u matematici :

1001 (binarno) = 1*2^3 + 0*2^2 + 0*2^1 + 1*2^0 = 9 (decimalno)