我写一个方法,你自己看着用:
// 将十进制数n转化为R进制,R<=36,因为大于三十六,没有字母可供表示,此处只考虑n
//为无符号数,返回值为 R进制表示的字符串
public String convert(int n, int R) {
int a = 0; // 商
int b = 0; // 余数
StringBuffer stringBuffer = new StringBuffer();
do {
a = n / R;
b = n % R;
if (b < 10) { // b为0-9
stringBuffer.append(b);
} else {
stringBuffer.append((char) ('A' + (b - 10)));
}
} while (a ==0);
StringBuffer sb=new StringBuffer();
for(int i=stringBuffer.length()-1;i>=0;i--) {
sb.append(stringBuffer.charAt(i));
}
return new String(sb);
}
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
System.out.print("输入十进制数:");
Scanner sc = new Scanner(System.in);
int _10n = sc.nextInt();
System.out.print("输入要转换的进制数:");
sc = new Scanner(System.in);
int radix = sc.nextInt();
String result = Integer.toString(_10n, radix);
System.out.println(result);
}
}