如何在 Java 中从任何基数转换为基数 10
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19607001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert from any base to base 10 in Java
提问by drgPP
i'm new at Java. I want to write a program that converts from base 2, 3, 4, 5, 6, 7, 8, 9, 16 to base 10 using only aritmetic operations.
我是 Java 新手。我想编写一个仅使用算术运算将基数 2、3、4、5、6、7、8、9、16 转换为基数 10 的程序。
I've done with reading a string from keyboard (in case if number is in hexadecimal) and converting it to an integer, after this i made a while loop what split number to digits and inverts them.
我已经完成了从键盘读取字符串(以防数字为十六进制)并将其转换为整数,然后我做了一个 while 循环,将数字拆分为数字并将它们反转。
Now i don't know how to make this digits to multiply with 2 at power 0, 1, 2, etc.(in binary case) to convert the number to base 10.
现在我不知道如何使这些数字与 2 的 0、1、2 次幂相乘(在二进制情况下)以将数字转换为基数 10。
For example 1001 (number 9 in decimal) it's like 1x2(pow 0)+0x2(pow 1)+0x2(pow 2)+1x2(pow 3).
例如 1001(十进制数字 9)它就像 1x2(pow 0)+0x2(pow 1)+0x2(pow 2)+1x2(pow 3)。
My code:
我的代码:
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;
}
}
}
采纳答案by drgPP
Use Integer.toString(int i, int radix)
:
使用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);
}
The reverse can be done with Integer.parseInt(String s, int radix)
:
可以通过以下方式完成相反的操作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);
}
回答by FazoM
Try something like this:
尝试这样的事情:
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);
}
}
If this is your class assessment then you should spend some time in trying to understand the code. You can replace pow() function by a built-in Java function if this is allowed.
如果这是您的课堂评估,那么您应该花一些时间尝试理解代码。如果允许,您可以用内置 Java 函数替换 pow() 函数。
回答by Manit Monsur
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);
}
}
回答by Karthick Kumar
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
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
}