CODEDIGEST
Home Articles CodeDigest Tutorials InstallShield FAQs
Skip Navigation LinksHome » Article » Csharp Article » Numeric Converter - Going Mobile!  Submit Articles and Win Geeky Prizes!!   You are not logged in.
Search
 

Sponsors
InstallShield
 

Product Spotlight
 

Technologies
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Numeric Converter – Going mobile!
Free Trial: InstallShield 2010 for Windows Installers Is InstallShield right for you? InstallShield handles your most complex installation requirements in minutes. Try it now.

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

Subscribe to our feed!

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

InstallShield

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:

Sponsors

Useful Books For Developers
C# 2008 and 2005 Threaded Programming: Beginner's Guide More books..

Similar Articles

 

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.

 

You can contribute to CodeDigest.Com:
Donate to CodeDigest.com
Article Feedback
Title  
Submitted By  
Comment  
Enter the verification number
 
Comments