Discuss / Java / 练习:stack将十进制数字转化为十六进制

练习:stack将十进制数字转化为十六进制

Topic source

jasmine

#1 Created at ... [Delete] [Delete and Lock User]
static String toHex(int n) {
    int i;    Stack<String> st = new Stack<String>();    Map<Integer, String> map = new HashMap<>();    for (int j = 0; j<10; j++) {
        map.put(j, j+"");    }
    map.put(10, "A");    map.put(11, "B");    map.put(12, "C");    map.put(13, "D");    map.put(14, "E");    map.put(15, "F");    do {
        i = n%16;        st.push(map.get(i));        n = n / 16;    } while (n != 0);    String result = "";    do {
        result = result.concat(st.pop());    } while (!st.empty());    return result;}

  • 1

Reply