Introduction
C# conversion functions are very powerful. Convert Class has many methods that perform typical conversion of a string in to integers, and other data types. One of the funuctions ToInt64() not only convert a given value to a 64-bit integer but also returns the number to the specified base.
Convert.ToInt64(value, 16);
converts a decimal number to hexa-decimal number. This is most useful in situations where you write programs for mathematical calculations.
I thought of writing my own functions that does this kind of conversion. I create two functions DecimalToBase which converts any decimal number to a binary,octal or hexa-decimal number; BaseToDecimal which converts any binary, octal, hexa-decimal number to the corresponding decimal number.
The most interesting part in writing these functions is that both the functions are generic in nature. In the DecimalToBase function, I had to write the algorithm that can perform conversion of a decimal to binary, octal and hexa-decimal system. I had to write the code for all of these conversions within a function, still keeping my code reduced to handle all sorts of values. C#’s type-safe nature prevents us to do some easier character-int conversions and we can’t just surpass them, as it would produce adverse results. Same is the case for the BaseToDecimal function also.
A sample application to test the conversion functions
1.Create a sample Windows Application in Visual Studio.Net.
2.In the form,
a.Add three label controls named label1, label2, and label3. b.Add three text box controls named textBox1, textBox2, and textBox3. c.Place a button control named button1 and set its Text property to “To Base”. d.Place another button control named button2 and set its Text property to “To Base”.
3.Write the following declarations as data members of the form class
const int base10 = 10; char[] cHexa = new char[]{'A','B','C','D','E','F'}; int[] iHexaNumeric = new int[] {10,11,12,13,14,15}; int[] iHexaIndices = new int[] {0,1,2,3,4,5}; const int asciiDiff = 48;
4.Include the functions as member methods in the form class module.
string DecimalToBase(int iDec, int numbase) { string strBin = ""; int[] result = new int[32]; int MaxBit = 32; for(; iDec > 0; iDec/=numbase) { int rem = iDec % numbase; result[--MaxBit] = rem; } for (int i=0;i if ((int)result.GetValue(i) >= base10) strBin += cHexa[(int)result.GetValue(i)%base10]; else strBin += result.GetValue(i); strBin = strBin.TrimStart(new char[] {'0'}); return strBin;}
int BaseToDecimal(string sBase, int numbase) { int dec = 0; int b; int iProduct=1; string sHexa = ""; if (numbase > base10) for (int i=0;i sHexa += cHexa.GetValue(i).ToString(); for(int i=sBase.Length-1; i>=0; i--,iProduct *= numbase) { string sValue = sBase[i].ToString(); if (sValue.IndexOfAny(cHexa) >=0) b=iHexaNumeric[sHexa.IndexOf(sBase[i])]; else b= (int) sBase[i] - asciiDiff; dec += (b * iProduct); } return dec; }
5.Write the code in the click event of the buttons as follows:
// On clicking the first button, you can convert a decimal number to any base you select in the combobox.
private void button1_Click(object sender, System.EventArgs e) { textBox3.Text = DecimalToBase(Int32.Parse(textBox1.Text), Int32.Parse(comboBox1.Text)); }
// On clicking the second button, you can convert a base number you select in the combobox (binary, octal, or hexa-decimal) to decimal number
private void button2_Click(object sender, System.EventArgs e) { textBox3.Text = BaseToDecimal(textBox1.Text, Int32.Parse(comboBox1.Text)).ToString(); }
6.Save the project. Build the solution.
|
No responses found. Be the first to respond and make money from revenue sharing program.
|