c# solution (negative, positive numbers)


  • 0
    H
        public string ToHex(int num)
        {
            string hex = string.Empty;
            uint unum = (uint)num;
    
            if (unum == 0)
            {
                return "0";
            }
    
            while (unum > 0)
            {
                uint y = unum % 16;
    
                if (y < 10)
                {
                    hex = (char)(y + '0') + hex;
                }
                else
                {
                    hex = (char)((y - 10) + 'a') + hex;
                }
    
                unum = unum >> 4;
            }
    
            return hex;
        }

Log in to reply
 

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