import java.util.Scanner;
public class TentoSixteen{
private static StringBuilder sum = new StringBuilder();
private static int rulst; //定义一个结果,记录数与15的与运算结果,求得原数二进制最后四位的值
private static int count; //若数的二进制不止四位,定义一个数来记录除去后四位的新数,运用递归作用与rulst相同
private static void TtoSMethod1(int i) { //JDK自带的转化方法
String exchanged = Integer.toHexString(i);
System.out.println(exchanged);
}
private static void Sop() { //将得到的字符串反转
System.out.println(sum.reverse().toString());
}
private static void subim(int rulst) {//判断是否超出10,根据十六进制的规则超出10部分由字母ABCDEF代替
if(rulst<10) {
sum.append(rulst);
}else {
sum.append((char)('A'+(rulst-10)));//将int强制转换为char
}
}
private static void TtoSMethod2(int i) {
rulst =i&15;
if(i<(1<<4)&&i>=0) {//判断数有多少位,大于0小于16,则转换后打直接印
subim(rulst);
}else {//不止四位的数则用递归来多次运算
count=i>>4;//除去最后四位
subim(rulst);
TtoSMethod2(count);//递归直到count为0止
}
}
public static void main(String []args) {
@SuppressWarnings("resource")
Scanner scan = new Scanner(System.in);
System.out.println("请出入一个十进制数:");
int num = scan.nextInt();
TtoSMethod1(num);//自带方法
TtoSMethod2(num);//自定义方法,可将任意的十进制转化为十六进制
Sop();//输出
}
}
public class Test {
public static void main(String[] args) {
int a=1000;
System.out.println(new Test().get16Jz(a));
}
public String get16Jz(int n){
if(n/16<16){
return getExp(n/16)+""+getExp(n%16);
}
return get16Jz(n/16)+""+n%16;
}
public String getExp(int a){
if(a<10){
return a+"";
}
if(a>=10){
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+"";
}
}
网上搜吧,一艘一大堆,何必来这里问呢