Click here to Skip to main content
15,889,738 members
Home / Discussions / C#
   

C#

 
AnswerRe: c# binary to decimal code -new- PinPopular
Peter_in_27805-Oct-11 20:18
professionalPeter_in_27805-Oct-11 20:18 
GeneralRe: c# binary to decimal code -new- Pin
mehmetali_066-Oct-11 20:47
mehmetali_066-Oct-11 20:47 
GeneralRe: c# binary to decimal code -new- Pin
Perić Željko7-Oct-11 7:47
professionalPerić Željko7-Oct-11 7:47 
GeneralRe: c# binary to decimal code -new- Pin
Peter_in_27807-Oct-11 11:49
professionalPeter_in_27807-Oct-11 11:49 
GeneralRe: c# binary to decimal code -new- Pin
KP Lee10-Oct-11 10:57
KP Lee10-Oct-11 10:57 
GeneralRe: c# binary to decimal code -new- Pin
Peter_in_278010-Oct-11 14:42
professionalPeter_in_278010-Oct-11 14:42 
GeneralRe: c# binary to decimal code -new- Pin
KP Lee10-Oct-11 15:05
KP Lee10-Oct-11 15:05 
NewsRe: c# binary to decimal code -new- Pin
Perić Željko7-Oct-11 7:56
professionalPerić Željko7-Oct-11 7:56 
Hello mehmetali,
peter have excelent solution ,
I made mine own which is not as elegant.
It is console application made in SharpDevelop C#.
This code is update of old one, it have no problem with entry of numbers as old version,
now it works with float point numbers and it folows full math logic.


C#
/*
 * 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: 1001) : ");
		      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 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("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;
			}
		}
	}
}


Code was updated 09/02/2012.
Whole Solution for IDE SharpDevelop you can download from the bottom of this page :


Conversion from one to another number system [ Open in new window ]

All the best,
Peric Zeljko
periczeljkosmederevo@yahoo.com

modified 26-Feb-12 8:50am.

AnswerRe: c# binary to decimal code -new- Pin
KP Lee10-Oct-11 14:51
KP Lee10-Oct-11 14:51 
GeneralRe: c# binary to decimal code -new- Pin
Peter_in_278011-Oct-11 12:41
professionalPeter_in_278011-Oct-11 12:41 
GeneralRe: c# binary to decimal code -new- Pin
KP Lee11-Oct-11 13:55
KP Lee11-Oct-11 13:55 
QuestionGood reporting and database tools for C# ? Pin
Stan Moong5-Oct-11 18:22
Stan Moong5-Oct-11 18:22 
AnswerRe: Good reporting and database tools for C# ? Pin
Eddy Vluggen6-Oct-11 6:55
professionalEddy Vluggen6-Oct-11 6:55 
GeneralRe: Good reporting and database tools for C# ? Pin
Stan Moong6-Oct-11 14:31
Stan Moong6-Oct-11 14:31 
QuestionCall Methods Across ActiveSync Connection Pin
namelkcip5-Oct-11 17:02
namelkcip5-Oct-11 17:02 
QuestionBasic c# question Pin
SFORavi5-Oct-11 13:22
SFORavi5-Oct-11 13:22 
AnswerRe: Basic c# question Pin
BillWoodruff5-Oct-11 14:08
professionalBillWoodruff5-Oct-11 14:08 
QuestionGetting system specs without WMI Pin
CCodeNewbie5-Oct-11 9:30
CCodeNewbie5-Oct-11 9:30 
AnswerRe: Getting system specs without WMI Pin
Luc Pattyn5-Oct-11 10:26
sitebuilderLuc Pattyn5-Oct-11 10:26 
GeneralRe: Getting system specs without WMI Pin
CCodeNewbie5-Oct-11 11:03
CCodeNewbie5-Oct-11 11:03 
GeneralRe: Getting system specs without WMI Pin
Dave Kreskowiak5-Oct-11 15:44
mveDave Kreskowiak5-Oct-11 15:44 
GeneralRe: Getting system specs without WMI Pin
CCodeNewbie5-Oct-11 19:25
CCodeNewbie5-Oct-11 19:25 
GeneralRe: Getting system specs without WMI Pin
Dave Kreskowiak6-Oct-11 3:39
mveDave Kreskowiak6-Oct-11 3:39 
QuestionFor Remote Connection -SQL Server Pin
Paramu19735-Oct-11 4:21
Paramu19735-Oct-11 4:21 
AnswerRe: For Remote Connection -SQL Server Pin
PIEBALDconsult5-Oct-11 5:22
mvePIEBALDconsult5-Oct-11 5:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.