十进制转换为任意进制
//将10进制数num转换为d进制数
void convert_base(int num, int d)
{
int k;
stack<int> stk;
while (num)
{
k = num % d;
num = num / d;
stk.push(k);
}
while (!stk.empty())
{
k = stk.top();
stk.pop();
printf("%x", k);
}
}
任意进制转换为十进制
#define MAXN 64 //64位CPU
//将s的d进制数转化为10进制数
int restore(const char s[MAXN], int d)
{
int res = 0;
int k;
for (int i = 0; s[i] != '\0'; i++)
{
if (s[i] >= '0' && s[i] <= '9') k = s[i] - '0';
else if (s[i] >= 'A' && s[i] <= 'Z') k = s[i] - 'A' + 10;
else k = s[i] - 'a' + 10;
res = res * d + k;
}
return res;
}