Real to Binary
Given that one of the interesting problem is transferring Real number to a binary 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: Perić Željko
* Date: 09.02.2012
* Time: 17:02
*
* Simple console program for conversion of
* Real to Binary numbers
* check out what is the local setting for decimal separator
* if it is "," everything is ok, if it is "." you need to correct program
* change in code "," into "." and ',' into '.'
*
*/
using System;
namespace Real_to_Binary
{
class Program
{
public static void Main(string[] args)
{
//
// declaration of variables
//
string [] DecimalFloat =new string [2];
string FloatPart = "";
string DecimalPart = "";
string Real = "";
string Binary = "";
string Letter = "";
string Sign = "";
double Cipher = 0;
double Integral = 0;
double RealNumber = 0;
double DivisionResult = 0;
double MultiplicationResult = 0;
double DecimalNumber = 0;
double FloatNumber = 0;
bool IsReal = false;
//
// not very desirable but necessary
// begining of program label
//
begining :
//
// Hello message to user
//
Console.Title = "Program Real to Binary ";
Console.SetWindowSize(80,40);
Console.ForegroundColor = ConsoleColor.Green;
Console.Clear();
Console.WriteLine("****************************************************************************");
Console.WriteLine(" Real to Binary");
Console.WriteLine("****************************************************************************");
//
// Get number from Console
//
Console.WriteLine();
Console.Write("Enter Real number (exmpl: 2,375 = 10,011) : ");
Real = "";
Real = Console.ReadLine();
Console.WriteLine();
Console.WriteLine("****************************************************************************");
Console.WriteLine();
//
// Check if it is Real number
//
IsReal = false;
RealNumber = 0;
IsReal = double.TryParse(Real,out RealNumber);
if (IsReal == true)
{
//
// clear sign plus or minus
//
if (Real.Contains("-"))
{
Real = Real.TrimStart('-');
Sign = "- ";
RealNumber = -1 * RealNumber;
}
else if (!Real.Contains("-") && !Real.Contains("+"))
{
Sign = "";
}
else if (Real.Contains("+"))
{
Real = Real.TrimStart('+');
Sign = "+ ";
RealNumber = double.Parse(Real.ToString());
}
//
// Clears number from 0
// if it is number with float point with all possibilites
//
// exmpl. 0001101,01010000 = 1101,0101
//
if ( Real.Contains(","))
{
DecimalFloat = Real.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")
{
Real = "0";
}
if (DecimalPart !="0" && FloatPart =="0")
{
Real = DecimalPart;
}
if (DecimalPart =="0" && FloatPart !="0")
{
Real = DecimalPart + "," + FloatPart;
}
if (DecimalPart !="0" && FloatPart !="0")
{
Real = DecimalPart + "," + FloatPart;
}
}
//
// if it is decimal number
//
else
{
Real = Real.TrimStart('0');
if (Real =="")
{
Real = "0";
}
}
}
else if (IsReal == false)
{
Console.WriteLine("Error , you didn't entered a number at all..");
Console.WriteLine();
//
// goto jump is not very desirable but necessary
//
goto end;
}
//
// Resolving conversion from Real to Decimal
// If it is Float number, split number to Decimal and Float part
// exmpl. 2,375(10) DecimalPart = 2 and FloatPart = 375
//
if (Real.Contains(","))
{
DecimalFloat = Real.Split(',');
//
// Decimal part first
//
DecimalPart = DecimalFloat[0];
DecimalNumber = double.Parse(DecimalPart);
Binary = "";
Integral = 0;
DivisionResult = 0;
MultiplicationResult = 0;
Cipher = 0;
while (DecimalNumber != 0 )
{
DivisionResult = DecimalNumber/ 2;
Integral = Math.Truncate(DivisionResult);
MultiplicationResult = 2 * Integral ;
Cipher = DecimalNumber - MultiplicationResult;
DecimalNumber = Integral;
Binary = Binary.Insert(0,Cipher.ToString());
}
//
// if DecimalPart number where zero
//
if (Binary == "")
{
Binary = "0";
}
DecimalPart = Binary;
//
// Float part second
//
FloatPart = "0," + DecimalFloat[1];
FloatNumber = double.Parse(FloatPart);
Binary = "";
Integral = 0;
MultiplicationResult = 0;
while (FloatNumber != 0)
{
MultiplicationResult = FloatNumber * 2;
Integral = Math.Truncate(MultiplicationResult);
FloatNumber = MultiplicationResult - Integral ;
Binary = Binary + Integral.ToString();
}
FloatPart = Binary;
Binary = "";
Binary = DecimalPart + "," + FloatPart;
//
// goto jump is not very desirable but necessary
//
//
goto write;
}
//
// Resolving conversion from Decimal to Binary
// If it is Decimal number
// exmpl. 1001
//
else
{
DecimalNumber = RealNumber;
Binary = "";
Integral = 0;
DivisionResult = 0;
MultiplicationResult = 0;
Cipher = 0;
while (DecimalNumber != 0 )
{
DivisionResult = DecimalNumber/ 2;
Integral = Math.Truncate(DivisionResult);
MultiplicationResult = 2 * Integral ;
Cipher = DecimalNumber - MultiplicationResult;
DecimalNumber = Integral;
Binary = Binary.Insert(0,Cipher.ToString());
}
//
// if entered number where zero
//
if (Binary == "")
{
Binary = "0";
}
}
write:
//
// Write result
//
if (RealNumber == 0)
{
Sign ="";
}
Console.WriteLine();
Console.WriteLine("Real number : " + Sign + Real);
Console.WriteLine();
Console.WriteLine("Binary number : " + Sign + Binary);
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