题解:
class Solution {
public:
string toHex(int _num) {
if (_num == 0) {
return "0";
}
std::string res;
long num = _num;
if (num < 0) {
num = pow(2, 32) + num;
}
while (num > 0) {
int val = num % 16;
char c = val + '0';
if (val >= 10) {
c = val - 10 + 'a';
}
res.push_back(c);
num = num/16;
}
reverse(res.begin(), res.end());
return res;
}
};