Java solution


  • 6
    Y
      public String toHex(int dec) {
          if (dec == 0) return "0";
          StringBuilder res = new StringBuilder();
          
          while (dec != 0) {
              int digit = dec & 0xf;
              res.append(digit < 10 ? (char)(digit + '0') : (char)(digit - 10 + 'a'));
              dec >>>= 4;
          }
          
        return res.reverse().toString();
      }
    

Log in to reply
 

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