class Solution {
public:
string toHex(int n) {
if(n == 0) return "0";
unsigned num = n;//转化成无符号数就是本身存储的内容,不同的命名方式只是对于存储的内容解释方式的不同。
string s;
while(num)
{
int c = num % 16;//
if(c < 10) s += c + '0';
else if(c >= 10) s += c - 10 + 'a';
num = num/16;
}
reverse(s.begin(), s.end());//逆置字符串,因为顺序是相反的。
return s;
}
};
405. 数字转换为十六进制数
最新推荐文章于 2025-04-20 23:04:05 发布