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

后端 未结 5 556
隐瞒了意图╮
隐瞒了意图╮ 2021-02-06 07:30

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

相关标签:
5条回答
  • 2021-02-06 07:39

    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);
        }            
    }
    
    0 讨论(0)
  • 2021-02-06 07:42
    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);
            }
        }
    
    }
    
    0 讨论(0)
  • 2021-02-06 07:44
    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.

    0 讨论(0)
  • 2021-02-06 07:46

    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?

    0 讨论(0)
  • 2021-02-06 07:47

    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.

    0 讨论(0)
提交回复
热议问题