数据结构(Java版)实验2:使用一个栈,将十进制转换成二进制、八进制或十六进制。

2025-01-02 07:12:48
推荐回答(1个)
回答1:

public static String baseConverter(int decNumber,int base){
String[] digits = new String[]{"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
String result = "";
Stack remStack = new Stack();
while(decNumber > 0){
int rem = decNumber % base;
remStack.push(rem);
decNumber = decNumber/base;
}
while(!remStack.isEmpty()){
result = result+digits[remStack.pop()];
}
return result;
}
这个可以任意转换,记得采纳啊