Convert a Number to Hexadecimal

https://www.lintcode.com/problem/convert-a-number-to-hexadecimal/description

public class Solution {
    /**
     * @param num: an integer
     * @return: convert the integer to hexadecimal
     */
    public String toHex(int num) {
        // Write your code here
        String res = "";
        while (res.length() < 8 && num != 0) {
            int mod = num & 15;
            if (mod < 10) {
                mod += '0';
            } else {
                mod = mod - 10 + 'a';
            }
            res = (char) mod + res;
            num >>= 4;
        }
        return res;
    }
}

你可能感兴趣的:(Convert a Number to Hexadecimal)