405. 数字转换为十六进制数

https://leetcode-cn.com/problems/convert-a-number-to-hexadecimal/

 

class Solution {
    public String toHex(int num) {
        if(num==0) return "0";
        StringBuilder sb = new StringBuilder();
        char[] chars={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
        while(sb.length()<8 && num!=0){
            sb.append(chars[num&0xf]);
            num >>= 4;
        }
        return sb.reverse().toString();
    }
}

  

上一篇:【巩固Java基础】String、StringBuffer、StringBuilder


下一篇:重学java——string & stringBuilder