Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Recursion Program - Convert a base 10 number to any Base

Tags:

java

recursion

I am trying to convert a base 10 number to any base by using conversion. Right now this is the code I have came up with. I have a sad feeling this may be completely wrong. The image below is an example of how this process should take place.

http://i854.photobucket.com/albums/ab107/tonytauart/rrrr.png

public static void main(String[] args) {


  int base;
  int number;


    Scanner console = new Scanner(System.in);

    System.out.println("Please enter the base");
        base = console.nextInt();
    System.out.println("Please enter the Number you would like to convert");
        number = console.nextInt();

        System.out.println(Converter(base, number));
}


public static int Converter(int Nbase, int Nnumber){

    int answer;
    int Rcontainer =0;
    int cnt = 0;
    int multiplier;
    int temp;
    double exp;

    if(Nnumber/Nbase == 0){
        cnt++;
        exp = Math.pow(10,cnt); 
        multiplier = (int)exp;
         answer = (Nnumber%Nbase)* multiplier + Rcontainer;


    }
    else
      {
       exp = Math.pow(10,cnt);
       multiplier = (int)exp;
       cnt++;
       temp = Rcontainer;
       Rcontainer = (Nnumber%Nbase)* multiplier + temp;   

       Nnumber = Nnumber/Nbase;
        answer = Converter(Nbase,Nnumber);
       }  
        return answer;
}

}

like image 369
user878034 Avatar asked Apr 17 '12 03:04

user878034


People also ask

How do you convert a number from any base to any base?

Step 1 − Divide the decimal number to be converted by the value of the new base. Step 2 − Get the remainder from Step 1 as the rightmost digit (least significant digit) of new base number. Step 3 − Divide the quotient of the previous divide by the new base.


3 Answers

I just finished doing this problem for a comp sci class. I had to solve this recursively:

public static String convert(int number, int base)
{
    int quotient = number / base;
    int remainder = number % base;

    if (quotient == 0) // base case
    {
        return Integer.toString(remainder);      
    }
    else
    {
        return convert(quotient, base) + Integer.toString(remainder);
    }            
}
like image 156
Ian Markind Avatar answered Nov 03 '22 00:11

Ian Markind


public class Converter {

    private static char symbols[] = new char[] { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T' };

    public static void main ( String args[] )
    {
                 Converter converter = new Converter ();
        System.out.println( converter.convert ( 31, 16 ));
    }

    public String convert ( int number, int base )
    {
        return convert(number, base, 0, "" );
    }

    private String convert ( int number, int base, int position, String result )
    {
        if ( number < Math.pow(base, position + 1) )
        {
            return symbols[(number / (int)Math.pow(base, position))] + result;
        }
        else
        {
            int remainder = (number % (int)Math.pow(base, position + 1));
            return convert (  number - remainder, base, position + 1, symbols[remainder / (int)( Math.pow(base, position) )] + result );
        }
    }
}

This will convert from Base 2 to Base 36, although you could expand it by adding more symbols.

like image 45
rKasun Avatar answered Nov 03 '22 00:11

rKasun


A quick way to do it in Java is this:

Integer.toString(int i,int radix);

For example,

Integer.toString(255,2)

would return "11111111". I'm not sure if you are just looking for a quick solution or do you actually want to implement the conversion method yourself. This would be a quick solution. Refer to this post: What is the method in the API for converting between bases?

like image 32
StephenChen Avatar answered Nov 03 '22 00:11

StephenChen