|
From: Bolek V. <vr...@pa...> - 2001-04-19 14:27:55
|
BTW, you can modify my function to revert the number in any base:
unsigned backwards( unsigned n, unsigned base) throw( invalid_argument)
// Reverse order of digits of n in base base
{
if (base < 2) throw invalid_argument( "Base must be > 1");
unsigned result = 0;
while (n > 0) {
result = base*result + n % base;
n = n / base;
}
return result;
}
Works for any base > 2, even for 312, for example.
However, it has a disadvantage to converting via string. If you have a
number like 12345000 and want 00054321, you get just 54321 - unless you
set the output field width properly which requires further algorithm.
--
============================================================
Ing. Boleslav Vrany
Stanice techniku DDM hl. m. Prahy
Pod Juliskou 2a
Praha 6 - Dejvice
16000
Czech Republic
tel. (420 2) 243 10 240
|