定义方法把十进制转换为十六进制

时间:2022-01-10 00:57:49
/*
 * 定义方法把十进制转换为十六进制
 */
public class Demo31 {
public static void main(String[] args) {
System.out.println(toshiliujinzhi(30));
}
public static String toshiliujinzhi(int num) {
String res="";
while(num!=0) {
int r=num%16;
res=dToH(r)+res;
num/=16;
}
return res;
}
public static String dToH(int n) {
if(n<10) {
return ""+n;
}
switch (n) {
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 "";
}
}