import java.util.Stack;
public class Code08 {
public static void main(String[] args) {
System.out.println(tenToOther(999,16));
System.out.println(OtherToTen("56",8));
//1001001从二进制转成16进制
System.out.println(tenToOther(OtherToTen("1001001",2),16));
}
public static String tenToOther(int N,int K){
Stack<Integer> stack = new Stack<>();
while (N != 0){
stack.push(N%(K));
N=N/(K);
}
String result = "";
while (!stack.isEmpty()){
int i = stack.pop();
if (i >= 10){
result += ((char)(i-10+'A'));
}else {
result += i;
}
}
return result;
}
public static int OtherToTen(String str,int K){
char[] chars = str.toCharArray();
Stack<Character> stack = new Stack<>();
for (int i = 0; i < chars.length; i++) {
stack.push(chars[i]);
}
int result = 0;
int p=0;
while (!stack.isEmpty()){
Character pop = stack.pop();
if(pop >= '0' && pop <= '9'){
result +=( pop-'0')*Math.pow(K,p++);
}else {
result += (pop-'A'+10)*Math.pow(K,p++);
}
}
return result;
}
}
11-13
1885
