in10se's method works, no doubt. It's very straightforward and follows directly from the definition of a written number as a polynomial. This is in fact how I implemented my first base-n converting program, back in Computer Science 101. But later on I came across Horner's method (currently a dead link in E2, but Horner's rule works), and I haven't looked back since.
In10se's approach is a bit cumbersome in that it calls for creating, at least temporarily, a table of powers of b, where b is the base. Done by hand, this calls for paper and scribbling thereon; done on a calculator, it requires memory slots.
With Horner's scheme, you can do the whole calculation with a series of additions and multiplications, without the need for additional storage. Also, you don't need to count up digits in the base b number; Horner works with numbers of arbitrary length, you just keep grinding the same two operations and just don't care about where it will end.
Enough propaganda! Here's where I put my money where my mouth is:
Algorithm to convert number xxx from base b to decimal
- Start with 0 as your working number.
- Repeat for each digit of xxx, working from left to right:
- Add that digit to your current working number.
- If you're not at the last digit
- multiply your working number by b
+ 3 = 3
× 8 = 24
+ 2 = 26
× 8 = 208
+ 7 = 215
× 8 = 1720
+ 6 = 1726
× 8 = 13808
+ 4 = 13812
On a simple-minded calculator that doesn't "do" algebraic notation, i.e. arithmetic precedence of multiplication over addition, you can bash this sequence in verbatim:
3 × 8 + 2 × 8 + 7 × 8 + 6 × 8 + 4 =
On a more sophisticated calculator, you need to insert some more equalses:
3 × 8 + 2 = × 8 + 7 = × 8 + 6 = × 8 + 4 =
In a pinch, if you don't have a calculator, you can probably do the calculation in your head: You only need to keep one "big" number in memory (apart from the original one); and so long as your base is less than 10, all your multiplication will be by a single digit number, which many people manage mentally. Failing that, it's easy enough to do with paper and pencil.
Simple, huh? Of course the credit goes to the bright folks who think this stuff up. In mathematics as in all of science, we are fortunate to be standing on the shoulders of giants.