Given an integer num, return a string representing its hexadecimal representation. For negative integers, two’s complement method is used.

All the letters in the answer string should be lowercase characters, and there should not be any leading zeros in the answer except for the zero itself.

Note:&nbsp;You are not allowed to use any built-in library method to directly solve this problem.

&nbsp;
Example 1:
Input: num = 26
Output: "1a"
Example 2:
Input: num = -1
Output: "ffffffff"

&nbsp;
Constraints:


	-231 &lt;= num &lt;= 231 - 1


class Solution:
    def toHex(self, num: int) -> str:
        ret = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]
        ans = ""

        if num < 0:
            num = pow(2,32) +num

        if num == 0:
            return "0"
        while num > 0:
            ans = ret[num%16] +ans
            num = num//16

        return ans
class Solution {
    public String toHex(int num) {
        if(num == 0) return "0";

        boolean start = true;

        StringBuilder sb = new StringBuilder();

        for(int i = 28; i >= 0; i -= 4) {
            int digit = (num >> i) & 15;
            if(digit > 9) {
                char curr = (char)(digit%10 + 'a');
                sb.append(curr);
                start = false;
            } else if(digit != 0) {
                char curr = (char)(digit + '0');
                sb.append(curr);
                start = false;
            } else {//digit == 0
                if(start == false) { //avoid case: 00001a
                    sb.append('0');
                }
            }

        }
        return sb.toString();
    }
}
class Solution
{
public:
    string toHex(int num)
    {
        string hex = "0123456789abcdef";
        unsigned int n = num; // to handle neg numbers
        string ans = "";
        if (n == 0)
            return "0";
        while (n > 0)
        {
            int k = n % 16;
            ans += hex[k];
            n /= 16;
        }
        reverse(ans.begin(), ans.end()); // as we stored it in the opposite order
        return ans;
    }
};
 var toHex = function(num) {
    let hexSymbols = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"];
    if (num >= 0) {
        let hex = "";
        do {
            let reminder = num % 16;
            num = Math.floor(num/16);
            hex = hexSymbols[reminder] + hex;
        } while (num > 0)
        return hex;
    } else {
        num = -num;
        let invertedHex = ""; //FFFFFFFF - hex
        let needToCarry1 = true; //adding + 1 initially and carrying it on if needed
        while (num > 0) {
            let reminder = num % 16;
            let invertedReminder = 15 - reminder; //inverting
            if (needToCarry1) { //adding 1 for 2's complement
                invertedReminder += 1;
                if (invertedReminder === 16) { //overflow, carrying 1 to the left
                    invertedReminder = 0;
                    needToCarry1 = true;
                } else {
                    needToCarry1 = false;
                }
            }
            num = Math.floor(num/16);
            invertedHex = hexSymbols[invertedReminder] + invertedHex;
        }
        //formatting as "FFFFFFFF"
        while (invertedHex.length < 8) {
            invertedHex = "f" + invertedHex;
        }
        return invertedHex;
    }
};
Convert a Number to Hexadecimal