codice:
#include <string>
#include <iostream>
using namespace std;
string converti10Beta(int n10, int base)
{
string res;
bool neg = false;
char alpha[] =
"0123456789ABCDEFGHIJKLMOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
if(n10 < 0) neg = true;
n10 = (n10 < 0) ? -n10 : n10;
while(n10 > 0)
{
res.insert(res.begin(), alpha[n10 % base]);
n10 /= base;
}
if(neg) res.insert(res.begin(), '-');
return res;
}
int convertiBeta10(string n, int base)
{
int res = 0, mult = 1;
bool neg = false;
string alpha =
"0123456789ABCDEFGHIJKLMOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
if(n[0] == '-') neg = true;
for(int i=n.size()-1; i>=0; --i)
{
int pos = alpha.find_first_of(n[i]);
if(pos == string::npos || pos >= base)
return ~0;
res += pos * mult;
mult *= base;
}
return (neg) ? -res : res;
}
Risolto
( (C) Cionci )