码迷,mamicode.com
首页 > 其他好文 > 详细

405. Convert a Number to Hexadecimal

时间:2018-05-26 21:17:45      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:str   color   lse   转换   convert   stat   abc   res   结果   

 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,把最右边四位摘下来,再对应出字符串中的字符,加至结果字符串左侧。

405. Convert a Number to Hexadecimal

标签:str   color   lse   转换   convert   stat   abc   res   结果   

原文地址:https://www.cnblogs.com/zhuangbijingdeboke/p/9094135.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!