Java - 递归程序 - 将基数为10的数字转换为任何基数

时间:2012-04-17 03:18:01

标签: java recursion

我正在尝试使用转换将基数为10的数字转换为任何基数。现在这是我提出的代码。我有一种悲伤的感觉,这可能是完全错误的。下图是此过程应如何进行的示例。

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

}

5 个答案:

答案 0 :(得分:12)

我刚刚为comp sci类完成了这个问题。我不得不递归地解决这个问题:

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

答案 1 :(得分: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 );
        }
    }
}

这将从Base 2转换为Base 36,尽管您可以通过添加更多符号来扩展它。

答案 2 :(得分:3)

在Java中快速实现这一目的是:

Integer.toString(int i,int radix);

例如,

Integer.toString(255,2)

将返回“11111111”。我不确定您是否只是在寻找快速解决方案,还是您真的希望自己实现转换方法。这将是一个快速的解决方案。请参阅此帖:What is the method in the API for converting between bases?

答案 3 :(得分:2)

如果您只想尝试转换基数(比如基数2),请尝试以下代码:

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

特别是基地2:

Integer.parseInt(Integer.toBinaryString(numberToConvert))

Integer包含其他方法,例如可以使用的toHexString。这些假设numberToConvert在基数10中。

答案 4 :(得分:1)

keytool -exportcert -list -v \
-alias <your-key-name> -keystore <path-to-production-keystore>