Hex Conversion

http://www.lintcode.com/zh-cn/problem/hex-conversion/

public class Solution {
    /**
     * @param n: a decimal number
     * @param k: a Integer represent base-k
     * @return: a base-k number
     */
    public String hexConversion(int n, int k) {
        // write your code here
        if (n == 0) {
            return "0";
        }
        String res = "";
        while (n > 0) {
            int temp = n % k;
            if (temp > 9) {
                char c = (char) ('A' + (temp - 10));
                res = c + res;
            } else {
                res = temp + res;
            }
            n = n / k;
        }
        return res;
    }
}

你可能感兴趣的:(Hex Conversion)