Write a function to convert an integer n into its ASCII string representation for a given base

The function must be implemented in a portable way that covers all possible values of n, and all bases between 2 and 16.

Download Desktop Version (requires .net)

 desktop

using System;
using System.Text ;

namespace IntegerToDecimal
{
/// <summary>
/// Summary description for Class1.
/// </summary>
  class Class1
  {
       /// <summary>
      /// The main entry point for the application.
      /// </summary>
     [STAThread]
     static void Main(string[] args)
     {
        //
        // TODO: Add code to start application here
       //
        Class1 theApp = new Class1();
        int num = -9999; // The base 10 number
        int basetoconvert = 16; // The base to which number is being converted 2-16
        Console.Out.WriteLine("Number ="+num+" Converted number : "+theApp.itoa(num,basetoconvert));
        Console.In.ReadLine();
    }

// The following function takes an integer and converts it to a base between 2 and 16 inclusive


public string itoa(int n, int basetoconvert)
{
    int temp = Math.Abs(n), digit,len,i;
    int base10;
    bool isneg;
    string tobinary = "";
    string tobase = "";
    int carry;

    // If numberis -ve set the flag to true
    isneg = ((n<0) ? true: false);
   // convert the base 10 number to base 2 (binary)
 
   do
   {
       digit = temp % 2;
       tobinary = digit.ToString() + tobinary;
       temp = temp / 2;
    }while (temp != 0);

    len = tobinary.Length;

    // We use a 16 bit string representaion, so if length of bit string is less than 16 pad it with 0's

    if (len < 16)
    for (i = 0; i < 16-len; i++)
       tobinary = 0.ToString() + tobinary;


       // if the number is negative get the 2's complement representation in binary
       // This is done by first getting the 1's complement by switching 1's to 0's and 0's to 1's
       // Adding 1 to 1's complement gives the 2's complement of binary number
      if (isneg==true)
      {
           StringBuilder tempbin = new StringBuilder(tobinary);
           tempbin.Replace('0','3');
           tempbin.Replace('1','0');
           tempbin.Replace('3','1');
          carry=1;
          tobinary = "";

         for (i=15;i>=0;i--)
        {
             temp = Int32.Parse(0.ToString()+tempbin[i]) + carry;

             if (temp==2)
            {
                 tobinary= "0" + tobinary ;
                  carry = 1;
            }
            else
            {
                 carry = 0;
                 tobinary = temp.ToString() + tobinary ;
            }

       }
    }


    // Once we have the binary representation of the original number we would convert it to base 10
     base10 = 0;
     i = 15;
     while (i >= 0)
    {
         base10 = base10 + Int32.Parse(0.ToString()+tobinary[i]) * (int) Math.Pow( 2.0, (double) (15-i));
         i--;
     }
     temp = base10;

     // From base 10, we can convert the original number to any other base
    do
   {
       digit = (int)temp % basetoconvert;
       if (digit < 10)
       tobase = digit.ToString() + tobase;
       else
      {
          switch (digit)
         {
               case 10 : tobase = "A" + tobase;
                              break;
              case 11 : tobase = "B" + tobase;
                             break;
              case 12 : tobase = "C" + tobase;
                             break;
              case 13 : tobase = "D" + tobase;
                            break;
              case 14 : tobase = "E" + tobase;
                            break;
              case 15 : tobase = "F" + tobase;
                            break;
          }
     }

      temp = temp / basetoconvert;
   }while (temp != 0);
   return tobase;
}

}
}