十进制和三十六进制的转换

package test;


public class JinZhi {


public static void main(String[] args) {
JinZhi jinZhi=new JinZhi();
System.out.println(jinZhi.bigToSmall("BEE"));
System.out.println(jinZhi.smallToBig(14774));
}
int bigToSmall(String str){
int num=0;
char []chs=str.toCharArray();
for(int j=0, i=chs.length-1;i>=0;i--){
if(chs[i]>='A'){
num+=(int) (((int)chs[i]-((int)'A')+10)*Math.pow(36, j)); 

}else{
num+=(int) (((int)(chs[i]-'0'))*Math.pow(36, j));
}
j++;
}
return num;
}
String smallToBig(int num){
StringBuffer buffer = new StringBuffer();
while(num>0){
//System.out.println(num%36);
if(num%36<=9){
buffer.insert(0,num%36);
//System.out.println((char)num%36);
}else{
buffer.insert(0,(char)((num%36-10)+(int)'A'));
//System.out.println((char)((num%36-10)+(int)'A'));
}
num/=36;
}

return buffer.toString();

}
}

你可能感兴趣的:(编程题)