在Java中从一种基础转换为另一种基础
发布于 2021-01-31 15:32:28
现在,我正在尝试找到一种方法,将数字从Java中的一个基数转换为另一个基数,给定一个数字,该数字所在的基数以及要转换为的基数。
public static void BaseConversion(String number, int base1, int base2){
//convert the number from one base to another
}
我找到了JavaScript的解决方案,并且想知道是否可以在Java中执行类似的操作:
function convertFromBaseToBase(str, fromBase, toBase){
var num = parseInt(str, fromBase); //convert from one base to another
return num.toString(toBase);
}
关注者
0
被浏览
92
1 个回答
-
你可以做
return Integer.toString(Integer.parseInt(number, base1), base2);
因此,使用您的函数签名,在Java中:
public String convertFromBaseToBase(String str, int fromBase, int toBase) { return Integer.toString(Integer.parseInt(str, fromBase), toBase); }
推荐阅读
-
使用DB Browser for SQLite从一种日期格式转换为另一种日期格式
2021-04-15 关注 0 浏览88 1答案
-
Java:如何将变量从一种类型动态转换为另一种类型?
2021-02-02 关注 0 浏览110 1答案
-
在 PHP 中将一种日期格式转换为另一种日期格式
2022-04-11 关注 0 浏览40 1答案
-
在PHP中将一种日期格式转换为另一种日期格式
2021-02-02 关注 0 浏览151 1答案
-
将值从一种形式传递到另一种形式
2021-02-02 关注 0 浏览533 1答案
-
将BufferedImage转换为另一种类型
2021-01-30 关注 0 浏览153 1答案
-
使用PHP从MySQL日期时间转换为另一种格式
2021-02-02 关注 0 浏览250 1答案
-
使用 PHP 从 MySQL 日期时间转换为另一种格式
2022-03-22 关注 0 浏览38 1答案
-
( )面谈是在一次面谈中,采取灵活变通的方式,从一种面谈形式转换过渡到另一种面谈形式。
2022-05-21 关注 0 浏览16 1答案
-
将日期时间转换为另一种格式而不更改dtype
2021-01-29 关注 0 浏览101 1答案