CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Numeric Converter – Going mobile!

By BalaMurali Balaji
Posted On Oct 24,2009
Article Rating:
Be first to rate
this article.
No of Comments: 5
Category: C#
Print this article.

Numeric Converter – Going mobile!

 

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

 

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

 

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

 

 

Introduction

The Numeric Converter allows you to convert a number of any bases to another base. The method DecimalToBase, it 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 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 un-necessary check for hexa-decimal 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));

 

Some sample outputs:

 

Converting a decimal to Binary number:

 

converter1.jpg

 

 

Converting a decimal to octal number:




 

converter2.jpg

 

Converting a binary to decimal number:

 

converter4.jpg

 

Converting an octal to binary number:

 

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 Windows Mobile Device Center.

 

Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments
Shiver me timbers, t
Shiver me timbers, them's some great intinmafoor. http://vxtsyplph.com [url=http://aymifirn.com]aymifirn[/url] [link=http://vigcwdlkka.com]vigcwdlkka[/link]
I could watch <a hre
I could watch <a href="http://qmsoqmwagh.com">Scirhdlen's</a> List and still be happy after reading this.
At last some ranttia
At last some ranttialioy in our little debate. http://zerhytkw.com [url=http://wnewfwnsjz.com]wnewfwnsjz[/url] [link=http://lcvkjedenwv.com]lcvkjedenwv[/link]
It's really great th
It's really great that people are sharing this <a href="http://amakaa.com">infrmoation.</a>
That's an innoigeus
That's an innoigeus way of thinking about it.