解题思路:
先用一个n保存10进制的原值,对16取余后将余值从头放入字符串,然后将n除以16,循环遍历直到n为0
代码:
#include<bits/stdc++.h>
using namespace std;
template <typename T>
std::string to_string(T value)//由于我用的Dev C++用不了to_string,就自己写了个
{
std::ostringstream os;
os << value;
return os.str();
}
string convertToBase16(int num) {
if(num == 0)
return "0";
string s;
int n = num;
if(num < 0)
n=-num;
while(n){
int temp = n % 16;
if(temp>9){
switch(temp){
case 10:
s.insert(0,"A");
break;
case 11:
s.insert(0,"B");
break;
case 12:
s.insert(0,"C");
break;
case 13:
s.insert(0,"D");
break;
case 14:
s.i