10 进制转 16 进制,计算公式如下例
* 1958 转 16 进制
* 1958 % 16 == 6 1958 / 16 == 122
* 122 % 16 == 10 也就是 A 122 / 16 == 7
public class Pretend {
public static String reverse(String s){
String r ="";
for(int i=s.length()-1;i>=0;i--) {
char ch =s.charAt(i);
r=r+ch;
}
return r;
}
public static String decToHex(int dec) {
String hex ="";
while(dec>=10) {
int remainder =dec%16;
dec=dec/16;
if(remainder<10) {
hex +=remainder;
}
else {
char ch=(char)('A'+(remainder-10));
hex+=ch;
}
}
if(dec!=0) {
hex+=dec;
}
return reverse(hex);
}
public static void main(String[] args) {
System.out.println(reverse("Hello"));
String hex=decToHex(176);
System.out.println(hex);
}
}