其他分享
首页 > 其他分享> > 405. 数字转换为十六进制数

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();
    }
}

  

标签:十六进制,return,int,StringBuilder,405,num,sb,转换
来源: https://www.cnblogs.com/longlyseul/p/15305062.html