405. Convert a Number to Hexadecimal

 1 static int wing=[]()
 2 {
 3     std::ios::sync_with_stdio(false);
 4     cin.tie(NULL);
 5     return 0;
 6 }();
 7 
 8 class Solution 
 9 {
10 public:
11     string toHex(int num) 
12     {
13         const string Hex="0123456789abcdef";
14         if(num==0)
15             return "0";
16         string res;
17         int count=0;
18         while(num&&count++<8)
19         {
20             res=Hex[num&0xf]+res;
21             num>>=4;
22         }
23         return res;
24     }
25 };

四位四位地转换,通过num&0xf,把最右边四位摘下来,再对应出字符串中的字符,加至结果字符串左侧。

猜你喜欢

转载自www.cnblogs.com/zhuangbijingdeboke/p/9094135.html
今日推荐