C# Solution


  • 0
    M
    public string ToHex(int num) {
        if (num == 0) return "0";
        string result = string.Empty;
        long max = 4294967296;
        long n = num; 
        
        if (n  < 0) n = max - Math.Abs(n);        
    
        while (n > 0)
        {
            long m = n % 16;
            if (m < 10) result = m.ToString() + result; 
            else result = Convert.ToChar('a' + m - 10) + result;
            n /= 16;
        }
    
        if (result.Length > 8) result = result.Substring(result.Length - 8);
    
        return result;
    }

Log in to reply
 

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