 Convert a Number to Hexadecimal

2017/12/13 posted in  leetcode

class Solution {
public:
    string toHex(int num) {
        const string HEX ="0123456789abcdef";
        if (num==0) return "0";
        string res ="";
        int count  = 0;
        while (num&&count++<8) {
            //&0xf表示取二进制的低四位
            res = HEX[(num&0xf)]+res;
            num>>=4;
        }
        return res;
    }
};