12,634,982 members (31,606 online)
Article
alternative version

6.6K views
4 bookmarked
Posted

# Numeric Converter – Going mobile!

, 3 Oct 2009 CPOL
 Rate this:
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.

```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.

```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'.

```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:

Converting a decimal to octal number:

Converting a binary to decimal number:

Converting an octal to a binary number:

## 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.

## Share

 Founder BB Systems CIT-GPNP India
No Biography provided

## You may also be interested in...

 Pro Pro