Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Converting from decimal to binary and back

This tutorial will show you how to convert from binary system (base 2) to decimal system (base 10) and back from decimal to binary. It provides a sample .NET 2.0 application which performs these conversions.

On Tuesday, August 9th 2005 at 09:30 AM
By Andrew Pociu (View Profile)
*****   (Rated 4.2 with 34 votes)
Contextual Ads
More C# Resources
Advertisement
Download this project


Download the application




You can download this project. It is developed in Visual Studio .NET 2005, using .NET Framework 2.0. However, the code can be used in .NET Framework 1.1 without any changes.

Here is how the application looks like in Windows Vista:







For more information on the binary numbering system read the Understanding Binary tutorial.


Converting from decimal to binary


Converting from decimal to binary involves a small algorithm that we need to use. There are multiple ways of converting an integer to binary, but I believe this one is the simplest to understand and implement:





private string ToBinary(Int64 Decimal)

{

   // Declare a few variables we're going to need

   Int64 BinaryHolder;

   char[] BinaryArray;

   string BinaryResult = "";

   while (Decimal > 0)

   {

      BinaryHolder = Decimal % 2;

      BinaryResult += BinaryHolder;

      Decimal = Decimal / 2;

   }


   // The algoritm gives us the binary number in reverse order (mirrored)

   // We store it in an array so that we can reverse it back to normal


   BinaryArray = BinaryResult.ToCharArray();

   Array.Reverse(BinaryArray);

   BinaryResult = new string(BinaryArray);


   return BinaryResult;

}




Converting from binary to decimal


Conversion from binary to decimal is done even easier. We don't need to use an algorithm because the .NET Framework provides us with a method called Convert.ToInt64() to which you pass a decimal.

You can see in the Binary Convertor project how we use this method to convert the text inside the TextBox to binary (and then back to string so that we can show it in the second TextBox):





txtDec2.Text = Convert.ToInt64(txtBin2.Text, 2).ToString();



Notice that we passed two parameters to the ToInt64() method. One is the actual number (retrieved from the TextBox), and one is the number 2 which represents the base (base 2, being the binary system).
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by Inkatu on Tuesday, August 9th 2005 at 02:10 PM

Ah, ha. Great Tutorial. Keep up the good work.

by mark on Friday, September 9th 2005 at 05:10 AM

watta tutorial!ur the best!

by Naveed on Sunday, October 9th 2005 at 01:39 PM

Wou! nice tutorial

by Elaine on Wednesday, October 12th 2005 at 01:09 PM

Very easy to understand - thank you!

by Mike on Sunday, November 27th 2005 at 12:01 AM

an easier way to convert to binary would be....

int X = textbox.text;
string binaryX = Convert.ToString(X, 2); //returns in base 2

good example though.

by Scott on Thursday, April 20th 2006 at 04:00 PM

It's good code, but if you enter numbers lower than 128, there's less than 8 digits.

by Mike on Monday, May 22nd 2006 at 01:01 PM

A simple fix for that...

while (X.Length < 8)
{
ab = "0" + ab;
}

by Scott on Monday, August 7th 2006 at 06:55 PM

or even simpler....

Convert.ToString(DecimalValue, 2).PadLeft(8, "0")

by MaryAnn on Thursday, August 24th 2006 at 06:24 AM

somehow i didnt like your site....sorry.

by Maresolaris on Tuesday, January 9th 2007 at 07:19 AM

Fantastic! Thanks for this info

by Ted on Thursday, February 1st 2007 at 03:33 PM

Nice tutorial. It illustrated to me a problem as simple as possible. Thanks!

by LB on Wednesday, August 15th 2007 at 04:13 PM

Useful information. Also, the Convert.ToString info is good to know. Thanks.

by Yuri on Thursday, September 20th 2007 at 09:02 PM

How i do this for a Cellphone ? in java ?

by soOs on Monday, October 8th 2007 at 05:39 PM

great site!!!!!!!!!!!!

by HSO on Sunday, December 16th 2007 at 11:13 PM

Thank You very much


VeryVeryVeryVeryVeryVeryVeryVeryVeryVery great site

by james on Tuesday, February 26th 2008 at 01:28 PM

How do i do binary to decimal and decimal to binary on paper? is there a formula?

by lyzh on Sunday, August 17th 2008 at 12:56 AM

.,how will i convert binary to decimal in c programming?

these are my code..theres something wrong in my formula ryt?? help me solve it..pls..

main()
{
int decimal,binaryholder,binaryresult;
clrscr();

printf("enter number in decimal: ");
scanf("%d",

by bitFlipper on Sunday, August 24th 2008 at 01:22 AM

nice job, but if you click on "convert" without putting anything in the textboxes, it will blow up... use try-catch or check for valid input before processing..

bf

by Sovan on Friday, March 20th 2009 at 03:42 AM

Binary to decimal:

binary : 11101
decimal=(start from left)

(pow(2,0) means 2 to the power 0)

1*pow(2,0) 0*pow(2,1) 1*pow(2,2) 1*pow(2,3) 1*pow(2,4)=29

decimal to binary

decimal= 29
binary:

29/2 qtnt=14 remainder=1
14/2 qtnt=7 remainder=0
7/2 qtnt=3 remainder=1
3/2 qtnt=1 remainder=1
1/2 qtnt=0 remainder=1
(have to carry this process untill qtnt become 0)
now go from down to top so the binary is : 11101

by Sovan on Friday, March 20th 2009 at 03:42 AM

Binary to decimal:

binary : 11101
decimal=(start from left)

(pow(2,0) means 2 to the power 0)

1*pow(2,0) 0*pow(2,1) 1*pow(2,2) 1*pow(2,3) 1*pow(2,4)=29

decimal to binary

decimal= 29
binary:

29/2 qtnt=14 remainder=1
14/2 qtnt=7 remainder=0
7/2 qtnt=3 remainder=1
3/2 qtnt=1 remainder=1
1/2 qtnt=0 remainder=1
(have to carry this process untill qtnt become 0)
now go from down to top so the binary is : 11101

by Sovan on Friday, March 20th 2009 at 03:42 AM

Binary to decimal:

binary : 11101
decimal=(start from left)

(pow(2,0) means 2 to the power 0)

1*pow(2,0) 0*pow(2,1) 1*pow(2,2) 1*pow(2,3) 1*pow(2,4)=29

decimal to binary

decimal= 29
binary:

29/2 qtnt=14 remainder=1
14/2 qtnt=7 remainder=0
7/2 qtnt=3 remainder=1
3/2 qtnt=1 remainder=1
1/2 qtnt=0 remainder=1
(have to carry this process untill qtnt become 0)
now go from down to top so the binary is : 11101

by Sovan on Friday, March 20th 2009 at 03:42 AM

Binary to decimal:

binary : 11101
decimal=(start from left)

(pow(2,0) means 2 to the power 0)

1*pow(2,0) 0*pow(2,1) 1*pow(2,2) 1*pow(2,3) 1*pow(2,4)=29

decimal to binary

decimal= 29
binary:

29/2 qtnt=14 remainder=1
14/2 qtnt=7 remainder=0
7/2 qtnt=3 remainder=1
3/2 qtnt=1 remainder=1
1/2 qtnt=0 remainder=1
(have to carry this process untill qtnt become 0)
now go from down to top so the binary is : 11101

by Sovan on Friday, March 20th 2009 at 03:42 AM

Binary to decimal:

binary : 11101
decimal=(start from left)

(pow(2,0) means 2 to the power 0)

1*pow(2,0) 0*pow(2,1) 1*pow(2,2) 1*pow(2,3) 1*pow(2,4)=29

decimal to binary

decimal= 29
binary:

29/2 qtnt=14 remainder=1
14/2 qtnt=7 remainder=0
7/2 qtnt=3 remainder=1
3/2 qtnt=1 remainder=1
1/2 qtnt=0 remainder=1
(have to carry this process untill qtnt become 0)
now go from down to top so the binary is : 11101

by specio on Monday, May 25th 2009 at 07:47 AM

fast and easy that is it what I'm looking for ;]

regards specio

by christine on Friday, July 17th 2009 at 08:12 PM

namimimimimimi michacao chipoe
as the decimal is..................

by christine on Friday, July 17th 2009 at 08:12 PM

namimimimimimi michacao chipoe
as the decimal is..................

by Muhammad Waqas on Sunday, October 4th 2009 at 03:57 AM

Very Nice Site.

But i need a simpler way to convert decimal and float value into binary in console application.

Can anyone share please.

by Navtej Singh on Friday, December 4th 2009 at 06:40 AM

very helpful code
thank you very much

by David on Saturday, January 23rd 2010 at 04:24 AM

if you guys need an online tool, here is a good decimal to binary converter
http://www.stringfunction.com/decimal-binary.html
David

by Bill on Wednesday, April 7th 2010 at 10:59 AM

Why the double Convert to cast a binary string to Decimal?

txtDec2.Text = Convert.ToInt64(Convert.ToInt64(txtBin2.Text, 2)).ToString();

why not just:
txtDec2.Text = Convert.ToByte(txtBin2.Text, 2).ToString();

issues with UNICODE ?

by Erich on Friday, April 16th 2010 at 08:36 AM

How about converting from Hexadecimal (string) to Binary(string) ?

string hexadecimalvalue;
string Binaryvalue;

by Bill on Friday, April 16th 2010 at 12:07 PM

@Erich: not sure you're responding to if so, and per my post, this:

txtDec2.Text = Convert.ToByte(txtBin2.Text, 2).ToString();

works just fine... question was, why is there a double cast involved a single would do the trick. Is there some idiosyncricy that I'm not seeing that makes the double cast important.

by Awais Ali Channa on Friday, May 13th 2011 at 05:12 AM

I have doing all practice and saw all program that is very best.

by Anter on Wednesday, June 8th 2011 at 06:32 PM

Thanks, this helped!

by jay on Wednesday, July 27th 2011 at 12:59 AM

nice site

by affordable SEO services on Monday, December 12th 2011 at 05:42 AM

Put everything in a method, and in the catch block you can recall the method (you can say the method calls itself). Use System.Windows.Forms.Cursor.Position to get the cursor position in screen coordinates.I admire the important information you offer within your content. I'll bookmark your web site and have my kids examine up the following typically.

by mortgage loans on Tuesday, December 27th 2011 at 02:59 PM

I received my first loan when I was 32 and that aided my relatives a lot. But, I require the consolidation loans as well.

by on Tuesday, January 17th 2012 at 12:42 AM

Once you have recreated the problem and captured these steps, you can save them to a file and send it to your support person, who can then open it up and view

by gd poker online on Sunday, January 22nd 2012 at 03:15 PM

What i don't understood is in fact how you're not really much more neatly-appreciated than you may be now. You are very intelligent. You already know therefore considerably in terms of this subject, made me in my view believe it from numerous varied angles. Its like women and men aren't fascinated until it is something to do with Lady gaga! Your own stuffs great. All the time handle it up!

by James on Saturday, June 2nd 2012 at 02:58 PM

this program is very good

by behnam on Wednesday, June 20th 2012 at 07:06 AM

private void btnDecToBin_Click(object sender, EventArgs e)
{
try
{
txtBin1.Text = ToBinary(Convert.ToInt64(txtDec1.Text));
}
catch (OverflowException Ex)
{
MessageBox.Show("The number you entered is too big.\r\n" Ex.Message, "Number too big", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private string ToBinary(Int64 Decimal)
{
// Declare a few variables we're going to needInt64 BinaryHolder;
//char[] BinaryArray;
string BinaryResult = "";

while (Decimal > 0)
{
BinaryHolder = Decimal % 2;
BinaryResult = BinaryHolder BinaryResult;
Decimal = Decimal / 2;
}

// The algoritm gives us the binary number in reverse order (mirrored)
// We store it in an array so that we can reverse it back to normal
//BinaryArray = BinaryResult.ToCharArray();
//Array.Reverse(BinaryArray);
//BinaryResult = new string(BinaryArray);

return BinaryResult;
}

by behnam on Wednesday, June 20th 2012 at 07:06 AM

private void btnDecToBin_Click(object sender, EventArgs e)
{
try
{
txtBin1.Text = ToBinary(Convert.ToInt64(txtDec1.Text));
}
catch (OverflowException Ex)
{
MessageBox.Show("The number you entered is too big.\r\n" Ex.Message, "Number too big", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private string ToBinary(Int64 Decimal)
{
// Declare a few variables we're going to needInt64 BinaryHolder;
//char[] BinaryArray;
string BinaryResult = "";

while (Decimal > 0)
{
BinaryHolder = Decimal % 2;
BinaryResult = BinaryHolder BinaryResult;
Decimal = Decimal / 2;
}

// The algoritm gives us the binary number in reverse order (mirrored)
// We store it in an array so that we can reverse it back to normal
//BinaryArray = BinaryResult.ToCharArray();
//Array.Reverse(BinaryArray);
//BinaryResult = new string(BinaryArray);

return BinaryResult;
}

by naman on Saturday, August 18th 2012 at 10:28 PM

so great AND helpful

by naman on Saturday, August 18th 2012 at 10:28 PM

so great AND helpful

by naman on Saturday, August 18th 2012 at 10:28 PM

so great AND helpful

by naman on Saturday, August 18th 2012 at 10:28 PM

so great AND helpful

by naman on Saturday, August 18th 2012 at 10:34 PM

please write something on submit button.click event
for stop for a while.

because it does not give fast response to reader that "Comment posted successfully!" and reader click
submit more than one time

thanks.
great site

by logmein on Tuesday, October 2nd 2012 at 02:32 AM

There are multiple ways of converting an integer to binary, but I believe this one is the simplest to understand and implement:

by mremote on Thursday, October 25th 2012 at 12:25 AM

converting an integer to binary, but I believe this one is the simplest to understand and implement:


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs C# Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Ads

From the creators of Geekpedia, a revolutionary new coupon website!

BargainEZ has coupons codes, printable coupons, bargains and it is the leading source of Passbook coupons for iPhone and iPod touch devices.

Coupons
Discover Geekpedia
Other Resources