Click here to Skip to main content
15,879,535 members
Articles / Desktop Programming / Windows Forms

Numeric Converter – Going mobile!

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
3 Oct 2009CPOL2 min read 9.5K   4  
A mobile application for converting numbers from one base to another.

Background

Four years ago, I wrote two methods in C#, one for the conversion of decimal to any base and another for converting any base to decimal.

It was also posted as an article in CodeProject titled: Conversion of fecimal to any base and vice-versa, and now, I have added an additional functionality for converting a number from any base to the other. For example, using this method, we could convert a binary number to an octal number. Accordingly, the UI is also changed.

This newer version of the Numeric Converter is available as a mobile device application, and is currently configured to run on a Windows Mobile 5.0 Pocket PC R2 device and the emulator.

Introduction

The Numeric Converter allows you to convert a number of any base to another base. The method DecimalToBase converts a number from decimal to the specified base entered by the user. Another method BaseToDecimal converts the number from the specified base to the corresponding decimal equivalent.

The following method is not actually modified from the previous version, and is left as it is to convert a number to any base the user has entered.

C#
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 < result.Length; 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;
}

The method BaseToDecimal is modified for there was an unnecessary check for hexadecimal input.

C#
int BaseToDecimal(string sBase, int numbase)
{
    int dec = 0;
    int b;
    int iProduct = 1;
    string sHexa = "";
    if (sBase.IndexOfAny(cHexa) >= 0)
        for (int i = 0; i < cHexa.Length; 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;
}

Both the methods are activated according to the user inputs, and invoked by pressing the Left Soft key for 'Convert'.

C#
if (comboBox1.SelectedItem.ToString() != "10" && comboBox2.SelectedItem.ToString() != "10")
{
    String temp = BaseToDecimal(textBox1.Text, Int32.Parse(comboBox1.Text)).ToString();
    textBox3.Text = DecimalToBase(Int32.Parse(temp), Int32.Parse(comboBox2.Text));
}
else if (comboBox2.SelectedItem.ToString() == "10")
    textBox3.Text = BaseToDecimal(textBox1.Text, Int32.Parse(comboBox1.Text)).ToString();
else if (comboBox1.SelectedItem.ToString() == "10")
    textBox3.Text = DecimalToBase(Int32.Parse(textBox1.Text), Int32.Parse(comboBox2.Text));

Here are some sample outputs:

Converting a decimal to binary number:

NumericConverter/converter1.jpg

Converting a decimal to octal number:

NumericConverter/converter2.jpg

Converting a binary to decimal number:

NumericConverter/converter4.jpg

Converting an octal to a binary number:

NumericConverter/converter5.jpg

Conclusion

This Numeric Converter is easy to install in your Pocket PC, and is available for download using the link given in the top of the article. The deployment of this application can be done through Visual Studio 2008, or you may manually transfer the files using the Windows Mobile Device Center.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder BB Systems CIT-GPNP
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --