static String toHex(int n) { // 创建一个栈 Deque<Integer> stack = new LinkedList<>(); /* * 1.初始化一个余数,一个整数商、 * 2.使用do-while判断整数商不为0,则继续执行取余,取商 * 3.出栈,返回 */ int ys = 0, zs = n; do { ys = zs % 16; zs = zs / 16; stack.offer(ys); } while (zs != 0); StringBuffer sb = new StringBuffer(); int times = stack.size(); for (int i = 0; i < times; i++) { sb.append(getTransToHexFmt(stack.pollLast())); } return sb.toString(); } static String getTransToHexFmt(Integer it) { switch (it) { case 10: System.out.println(it); return "a"; case 11: System.out.println(it); return "b"; case 12: System.out.println(it); return "c"; case 13: System.out.println(it); return "d"; case 14: System.out.println(it); return "e"; case 15: System.out.println(it); return "f"; default: return it.toString(); } }
Sign in to make a reply
ANGERIED