java十进位转换十四进位代码该怎麼写?求大神,谢谢
发布网友
发布时间:2022-11-02 03:21
我来回答
共3个回答
热心网友
时间:2023-10-22 10:25
以下算法可以转36进制以下的任何进制。
import java.math.BigInteger;
public class Test {
public static String baseString(int num, int base) {
String str = "", digit = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (num == 0) {
return "";
} else {
str = baseString(num / base, base);
return str + digit.charAt(num % base);
}
}
public static String baseString(BigInteger num, int base) {
String str = "", digit = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if (num.shortValue() == 0) {
return "";
} else {
BigInteger valueOf = BigInteger.valueOf(base);
str = baseString(num.divide(valueOf), base);
return str + digit.charAt(num.mod(valueOf).shortValue());
}
}
public static void main(String[] args) {
System.out.println(baseString(1295, 36));
BigInteger big = new BigInteger("28");
System.out.println(baseString(big, 14));
}
}
热心网友
时间:2023-10-22 10:25
转14不是16?
16进制的话
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请出入一个十进制数");
int x = sc.nextInt();
System.out.println(x+"的十六进制结果是:"+Integer.toHexString(x));
}
}
热心网友
时间:2023-10-22 10:26
bigint.tostring