如何在Java中从任何基数转换为基数10

时间:2013-10-26 12:58:30

标签: java

我是Java新手。我想编写一个程序,只使用aritmetic操作将基数2,3,4,5,6,7,8,9,16转换为基数10。

我已经完成了从键盘读取字符串(如果数字是十六进制)并将其转换为整数,之后我做了一个while循环,将数字拆分为数字并反转它们。

现在我不知道如何使这个数字在幂0,1,2等处乘以2(在二进制情况下)以将数字转换为基数10.

例如1001(十进制数字9),它就像1x2(pow 0)+ 0x2(pow 1)+ 0x2(pow 2)+ 1x2(pow 3)。

我的代码:

public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Introduceti din ce baza doriti sa convertiti numarul: 2, 3, 4, 5, 6, 7, 8, 9, 16 ");
    int n = Integer.parseInt(br.readLine());
    Scanner scanner = new Scanner(System.in);
    System.out.println("Introduceti numarul care doriti sa fie convertit din baza aleasa ");
    String inputString = scanner.nextLine();
    if (n==2){
        int conv = Integer.parseInt(inputString);
        while (conv>0){
            System.out.println (conv%10);
            conv = conv/10;        
        }
    }
}

4 个答案:

答案 0 :(得分:14)

使用Integer.toString(int i, int radix)

int i = 1234567890;
for (int base : new int[] { 2, 3, 4, 5, 6, 7, 8, 9, 16}) {
  String s = Integer.toString(i, base);
}

可以使用Integer.parseInt(String s, int radix)完成相反的操作:

String s = "010101";
for (int base : new int[] { 2, 3, 4, 5, 6, 7, 8, 9, 16}) {
  Integer i = Integer.parseInt(s, base);
}

答案 1 :(得分:1)

尝试这样的事情:

class Bases
{
    public static void main(String[] args)
    {
            //tests
        String l1 = "01010101"; //base 2, 85
        String l2 = "123123123"; // base 4, 112347
        String l3 = "FFFF"; //base 16, 65535

        System.out.println(rebase(l1,2));
        System.out.println(rebase(l2,4));
        System.out.println(rebase(l3,16));
    }

    //symbols array
    private static final String SYMBOLS = "0123456789ABCDEF";

    //actual algorithm 
    public static long rebase(String number, int base)
    {
        long result = 0;
        int position = number.length(); //we start from the last digit in a String (lowest value)
        for (char ch : number.toCharArray())
        {
            int value = SYMBOLS.indexOf(ch);
            result += value * pow(base,--position); //this is your 1x2(pow 0)+0x2(pow 1)+0x2(pow 2)+1x2(pow 3)

        }
        return result;
    }

    //power - don't know if this is needed?
    private static long pow(int value, int x)
    {
        if (x == 0) return 1;
        return value * pow(value,x-1);
    }
}

如果这是您的课堂评估,那么您应该花一些时间来尝试理解代码。如果允许,可以通过内置的Java函数替换pow()函数。

答案 2 :(得分:0)

Consider an example,  
Convert (235) base 8 into base 10.

5 x 8^0 = 5  
3 x 8^1 = 24   
2 x 8^2 = 128   
Now simply add these values together.   
5 + 24 + 128 = 157  
Answer: (235)base 8 = (157)base 10  
For more example, refer [This URL][1]  

http://mathbits.com/MathBits/CompSci/Introduction/tobase10.htm

public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    String s = in.next(); //235
    int b = in.nextInt(); //8 
    int result = getBase10(s, b); //getBase10("235",8);
    System.out.println(result);
}    

private static int getBase10(String s, int b) {

    int base = 0, pow = 0;
    int[] a = new int[s.length()];

    for (int i = 0; i < s.length(); i++) {
        a[i] = s.charAt(i) - '0'; //Convert into int array
    }

    for (int i = a.length - 1 ; i >= 0 ; i--) {
        base += a[i] * Math.pow(b,pow); //Generalised formula for conversion  
        pow++;  
    }
    System.out.println("Base 10 : "+base); // base = 157  
    return base; //157  
}

答案 3 :(得分:-1)

public class base2ToBase10Conversion {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Input Base 2 value:");
        int a = input.nextInt();
        int b = a/10000000;
        double c = b* Math.pow(2, 7);
        int d = Math.abs(a-(10000000*b));
        int e = d/1000000;
        double f = e* Math.pow(2,6);
        int g = Math.abs(d-(1000000*e));
        int h = g/100000;
        double i = h * Math.pow(2,5);
        int j = Math.abs(g-(100000*h));
        int k = j/10000;
        double l = k * Math.pow(2,4);
        int m = Math.abs(j-(10000*k));
        int n = m/1000;
        double o = n * Math.pow(2, 3);
        int p = Math.abs(m-(1000*n));
        int q = p/100;
        double r = q* Math.pow(2, 2);
        int s = Math.abs(p-(100*q));
        int t = s/10;
        double u = t* Math.pow(2,1);
        int v = Math.abs(s-(10*t));

        double base10 = c + f + i + l + o + r + u + v;

        System.out.println("Valuse in Base 10: " + base10);
    }
}