building a decimal-binary-hexadecimal converter! [Tutorial]

Today, we’ll build a multipurpose converter.

Decimal – Binary – Hexadecimal

Students are asked to build this program in their first week of school so I thought a lot of my readers would enjoy this.

With this program you’ll be able to convert from all three formats to all three formats. Each conversion is in it’s own method so you can easily see how it works.

Have fun guys!

Our Converter.

This is how our final solution will look like.

We’ll use:

  • Labels
  • Textboxes
  • Buttons
  • Class Instancing
  • Number Formatting

Download the final solution here.

The complete tutorial, after the break.

So let’s get started.

Create a class called ConversionFormulas. We’ll hold all of our formulas in this class.


#region “Decimal Conversions”
public string DecimaltoBinary(TextBox txtdecimal, TextBox txtbinary)
{
Int32 DecimalNumber = Convert.ToInt32(txtdecimal.Text);
Int64 BinaryNumber;
char[] BinaryArray;
string BinaryResult = “”;

while (DecimalNumber > 0)
{

BinaryNumber = DecimalNumber % 2;
BinaryResult += BinaryNumber;

DecimalNumber = DecimalNumber / 2;
}

BinaryArray = BinaryResult.ToCharArray();
Array.Reverse(BinaryArray);

BinaryResult = new string(BinaryArray);

return BinaryResult;
}

public string DecimaltoHexadecimal(TextBox txtdecimal, TextBox txthexadecimal)
{
int DecimalValue = Convert.ToInt32(txtdecimal.Text);
string HexValue = DecimalValue.ToString(“X”);
return HexValue;
}
#endregion

#region “Binary Conversions”
public string BinarytoDecimal(TextBox txtdecimal, TextBox txtbinary)
{
return Convert.ToInt64(txtbinary.Text, 2).ToString();
}
#endregion

#region “Hexadecimal Conversions”
public string HexadecimaltoDecimal(TextBox txtdecimal, TextBox txthexadecimal)
{
int DecimalValue = int.Parse(txthexadecimal.Text, System.Globalization.NumberStyles.HexNumber);
string DecimalString = DecimalValue.ToString();

return DecimalString;
}
#endregion

Notice that all of our methods return a string value at the end. This is so we can easily place that string to a textBox of our chosing.

Go back to Form1 and create an instance of the previous class so we can access it’s methods.

Conversion_Formulas oFormulas = new Conversion_Formulas();

Next we have to program our three buttonClick events in Form1. Double click on each button and copy the following code:

private void button1_Click(object sender, EventArgs e)
{
textBox2.Text = oFormulas.DecimaltoBinary(textBox1, textBox2);
textBox3.Text = oFormulas.DecimaltoHexadecimal(textBox1, textBox3);
}

private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = oFormulas.BinarytoDecimal(textBox1, textBox2);
textBox3.Text = oFormulas.DecimaltoHexadecimal(textBox1, textBox3);

}

private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = oFormulas.HexadecimaltoDecimal(textBox1, textBox3);
textBox2.Text = oFormulas.DecimaltoBinary(textBox1, textBox2);
}

Notice that when we call on each method we have to pass parameters to it, we have to let the method know what textBoxes to work with.

That’s about it. I hope you guys enjoyed this tutorial, if you have any question feel free to ask in the comments.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s