Discuss / Java / 进制转换

进制转换

Topic source

static String toHex(int n) {    if (n == 0) {        return "0";    }    Deque<Character> stack = new LinkedList<>();    char[] hexChars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};    while (n != 0) {        stack.push(hexChars[n % 16]);        n = n / 16;    }    StringBuilder sb = new StringBuilder();    while (!stack.isEmpty()) {        sb.append(stack.pop());    }    return sb.toString();}


static String toHex(int n) {    if (n == 0) {        return "0";    }    Deque<Character> stack = new LinkedList<>();    char[] hexChars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};    while (n != 0) {        stack.push(hexChars[n % 16]);        n = n / 16;    }    StringBuilder sb = new StringBuilder();    while (!stack.isEmpty()) {        sb.append(stack.pop());    }    return sb.toString();}

static String toHex(int n) {
     if (n == 0) {
         return "0";
}
     Deque<Character> stack = new LinkedList<>();
     char[] hexChars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
while (n != 0) {
         stack.push(hexChars[n % 16]);
n = n / 16;
}

     StringBuilder sb = new StringBuilder();
while (!stack.isEmpty()) {
         sb.append(stack.pop());
}
     return sb.toString();
}


  • 1

Reply