赞
踩
直接调用Integer.toString方法(public static String toString(int i,int radix))
import java.util.*;
public class Test {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
//将输入的数字n转化为16进制数
String ret = Integer.toString(n, 16);
System.out.println(ret);
}
}
输入10
输出a
使用Integer.parseInt方法(public static int parseInt(String s,int radix)),
举例:
public class Test {
public static void main(String[] args) {
String a = "1001";
int s = Integer.parseInt(a, 2);
System.out.println(s);
}
}
import java.util.*; public class Test { public static String jinZhiZhuanHuan(String num,int n,int b) { String a = num; int s = Integer.parseInt(a, n); String ret = Integer.toString(s, b); return ret; } public static void main(String[] args) { Scanner scan = new Scanner(System.in); String num=scan.next(); int n = scan.nextInt(); int b=scan.nextInt(); String ret=jinZhiZhuanHuan(num,n,b); System.out.println(ret); } }
public class Test { //设置字符数组 //可以添加任意不重复字符,提高能转换的进制的上限 static char[] chs = new char[36]; static { for(int i = 0; i < 10 ; i++) { chs[i] = (char)('0' + i); } for(int i = 10; i < chs.length; i++) { chs[i] = (char)('A' + (i - 10)); } } /*num 源数据字符串 fromRadix 源数据的进制类型 toRadix 源标进制类型*/ static String transRadix(String num, int fromRadix, int toRadix) { int number = Integer.valueOf(num, fromRadix); StringBuilder scan = new StringBuilder(); while (number != 0) { scan.append(chs[number%toRadix]); number = number / toRadix; } return scan.reverse().toString(); } //测试 public static void main(String[] args) { System.out.println(transRadix("10", 10, 16)); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。