public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(tenTo16("1000"));
System.out.println(tenTo8("1000"));
System.out.println(twoTo10("11111111111111110"));
}
public static String tenTo16(String num){
int ten = Integer.valueOf(num);
StringBuilder res = new StringBuilder();
while(ten>0){
String tmp;
if(ten %16 == 10){
tmp = "A";
}else if(ten %16 == 11){
tmp = "B";
}else if(ten %16 == 12){
tmp = "C";
}else if(ten %16 == 13){
tmp = "D";
}else if(ten %16 == 14){
tmp = "E";
}else if(ten %16 == 15){
tmp = "F";
}else{
tmp = ten %16+"";
}
res.append(tmp);
ten /= 16;
}
res.reverse();
return res.toString();
}
public static String tenTo8(String num){
int ten = Integer.valueOf(num);
StringBuilder res = new StringBuilder();
while(ten>0){
res.append(ten%8);
ten /= 8;
}
res.reverse();
return res.toString();
}
public static String twoTo10(String two){
char[] ch = two.toCharArray();
int res = 0;
int j = 0;
if(ch[ch.length-1] == '1'){
res = 1;
}else if(ch[ch.length-1] != '0'){
return "error";
}
for(int i=ch.length-2;i>=0;i--){
if(ch[i] == '0' || ch[i] == '1'){
res += Math.pow(2<<j, ch[i]-'0');
j++;
}else{
return "error";
}
}
return res+"";
}
有些没有判断非法输入