在Java中从一个基数转换为另一个基数 [英] Convert from one base to another in Java
本文介绍了在Java中从一个基数转换为另一个基数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
现在,我正在尝试找到一种方法,在Java中将数字从一个基数转换为另一个数字,给出一个数字,数字所在的基数,以及转换为的基数。
Right now, I'm trying to find a way to convert a number from one base to another in Java, given a number, the base that the number is in, and the base to convert to.
public static void BaseConversion(String number, int base1, int base2){
//convert the number from one base to another
}
我找到了JavaScript的解决方案,我想知道是否可以在Java中做类似的事情:
I found a solution for JavaScript, and I'm wondering if it would be possible to do something similar in Java:
function convertFromBaseToBase(str, fromBase, toBase){
var num = parseInt(str, fromBase); //convert from one base to another
return num.toString(toBase);
}
推荐答案
你可以做
return Integer.toString(Integer.parseInt(number, base1), base2);
因此,使用您的函数签名,在Java中:
So with your function signature, in Java:
public String convertFromBaseToBase(String str, int fromBase, int toBase) {
return Integer.toString(Integer.parseInt(str, fromBase), toBase);
}
这篇关于在Java中从一个基数转换为另一个基数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文