1 进制转换 10进制转换为16进制
package dianxin;
import java.util.Scanner;
public class 进制 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String str = sc.nextLine();
int num = Integer.parseInt(str);
String result = Integer.toHexString(num);
System.out.println(result);
}
}
}
//
十进制转化为十六进制,结果为C8。
Integer.toHexString(200);
//
十六进制转化为十进制,结果140。
Integer.parseInt("8C",16);
不知道为啥,没有AC,然后自己写了方法
package dianxin;
import java.util.Scanner;
public class jinzhi{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String str = sc.nextLine();
int num = Integer.parseInt(str);
//String result = Integer.toHexString(num);
t1(num);
//System.out.println(result);
}
}
private static int t1(int a){
String str = "";
//1:用a去除以16,得到商和余数
int s = a / 16;
int yushu = a % 16;
str = "" + shuZiToZiMu(yushu);
while(s>0){
//2:继续用商除以16 得到商和余数
yushu = s % 16;
s = s / 16;
//3:如果商为0,那么就终止
//4:把所有的余数倒数排列
str = shuZiToZiMu(yushu) +str;
}
System.out.println(str);
//将16进制转化为10进制
return Integer.parseInt(str,16);
}
private static String shuZiToZiMu(int a){
switch(a){
case 10 : return("A");
case 11 : return("B");
case 12 : return("C");
case 13 : return("D");
case 14 : return("E");
case 15 : return("F");
}
return ""+a;
}
}