栈是一种 先进后出 的数据结构。
import java.util.Stack;
/**
* @author: Cecurio
* @create: 2018-03-14 14:17
**/
public class Dex2Hex {
public static void main(String[] args) {
System.out.println(decimalism2otherSystem(51,16));
System.out.println(decimalism2otherSystem(51,2));
System.out.println(decimalism2otherSystem(51,8));
}
/**
* 10进制 转换成 其他进制
* @param num 十进制数
* @param system 其他的进制
* @return
*/
static String decimalism2otherSystem(int num, int system) {
Stack<String> stack = new Stack<>();
int n = num;
while (n != 0) {
int t = n % system;
switch (t) {
case 10:
stack.push("A");
break;
case 11:
stack.push("B");
break;
case 12:
stack.push("C");
break;
case 13:
stack.push("D");
break;
case 14:
stack.push("E");
break;
case 15:
stack.push("F");
break;
default:
stack.push("" + t);
break;
}
n = n / system;
}
StringBuffer sf = new StringBuffer();
if (system == 16) {
sf.append("0x");
}
while (!stack.empty()) {
sf.append(stack.pop());
}
return sf.toString();
}
}