7

我正在尝试使用转换将基数为 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;
}

}

4

5 回答 5

13

我刚刚为一个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);
    }            
}
于 2013-09-29T06:50:02.080 回答
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,尽管您可以通过添加更多符号来扩展它。

于 2012-04-17T06:02:10.940 回答
3

在 Java 中实现它的一种快速方法是:

Integer.toString(int i,int radix);

例如,

Integer.toString(255,2)

将返回“11111111”。我不确定您是否只是在寻找快速解决方案,或者您是否真的想自己实现转换方法。这将是一个快速的解决方案。参考这篇文章:API中的base之间转换的方法是什么?

于 2013-11-16T23:32:53.900 回答
2

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

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

对于特别基地2:

Integer.parseInt(Integer.toBinaryString(numberToConvert))

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

于 2012-12-12T05:48:25.840 回答
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);
        }
    }

}
于 2016-09-27T18:15:17.770 回答