public static void main(String[] args) {
System.out.println(dec2Hex(1000));
}
public static String dec2Hex(int dec){
Stack<String> s = new Stack<String>();
while(dec != 0){
int r = dec%16;
if(r>=10){
char c = getChar(r);
s.push(c+"");
}
else{
s.push(""+r);
}
dec = dec/16;
}
String result = "";
while(s.size()>0){
result += s.pop();
}
return result;
}
public static char getChar(int t){
switch(t){
case 10:return 'a';
case 11:return 'b';
case 12:return 'c';
case 13:return 'd';
case 14:return 'e';
default: return 'f';
}
} |