5

我是 Java 新手。我想编写一个仅使用算术运算将基数 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

4 回答 4

16

使用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);
}
于 2013-10-26T13:05:40.230 回答
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() 函数。

于 2013-10-26T13:56:29.880 回答
1
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  
}
于 2017-05-26T19:06:08.503 回答
-2
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);
    }
}
于 2015-08-30T12:06:17.380 回答