6 lines Java solution


  • 0
    S
    public String toHex(int num) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 8 && num != 0; i++) {
            int n = (num & 0xf);
            sb.insert(0, n < 10 ? (char) (n + '0') : (char) (n - 10 + 'a'));
            num >>= 4;
        }
        return sb.length() == 0 ? "0" : sb.toString();
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.