8

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;
}

}

6
  • Is this homework? If so, it should be labeled with the [homework] tag. Commented Apr 17, 2012 at 3:20
  • you should define a base case, let's say, when your number is less than your base, and the recursive part, let's say, the operations to get the next digit of your converted number Commented Apr 17, 2012 at 3:21
  • thanks for the heads up louis... and thanks as well luiggi, Idk why I thought having ==0 was a good idea. I don't think that is even needed now that I look at it Commented Apr 17, 2012 at 3:35
  • Are there restrictions on "any base"? For instance, are you expected to support base 900 (for which no standard character set is defined)? Commented Apr 17, 2012 at 4:11
  • I found an answer that explains how to convert an integer from any base to any other base: stackoverflow.com/a/15735138/975097 Commented Apr 1, 2013 at 2:11

5 Answers 5

14

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);
    }            
}
Sign up to request clarification or add additional context in comments.

1 Comment

I think we were learning from the same book. This is exactly what I needed!
10
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.

Comments

3

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?

1 Comment

However, the second argument defaults to 10 if given > Character.MAX_RADIX, which is usually 36 (10 for 0-9, 26 for alphabet) for the second argument.
2

If you are just trying to convert bases (like to base 2), try the following code:

Integer.parseInt(Integer.toString(numberToConvert,base))

For specifically base 2:

Integer.parseInt(Integer.toBinaryString(numberToConvert))

Integer contains other methods such as toHexString that can be used. These assume that numberToConvert is in base 10.

Comments

1
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);
        }
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.