Base conversion program

So I kind of want to make a basic base conversion program just for the hell of it. I'm not really sure how to go about doing it though. I can convert a number of any base to any other base just fine on paper, just not sure how to do it in a program.
1) Explain, in very simple step-by-step instruction form, the process of how you do it on paper
2) Translate that explanation into C++ code.
3) ???
4) Profit
Last edited on
Well, with how I do it on paper, I feel like I'd have to pick apart a string representation of a number first. Here's how I do it:
From base 10 to octal for example:
8526 / 8 = 1065 remainder 6
1065 / 8 = 133 remainder 1
133 / 8 = 16 remainder 5
16 / 8 = 2 remainder 0
2 / 8 = 0 remainder 2

So, 8526 base 10 = 20516 base 8
Base 10 to base x is simple enough. But base x to base 10 seems complicated to me, here's how I do it:
For simplicity, we'll use binary to base 10, but the method stays the same through out.
11010011 to base 10
Starting from the 2^0 bit, I move to the left till Im out of digits.

(2^0) x 1 = 1
(2^1) x 1 = 2
(2 ^2)x 0 = 0
(2 ^3)x 0 = 0
(2 ^4)x 1 = 16
(2 ^5)x 0 = 0
(2 ^6)x 0 = 64
(2 ^7)x 0 = 128

And now I just add up these products to get my number in base 10
1 + 2 + 16 + 64 + 128 = 211 base 10


It's this conversion from base x to base 10 that I'm struggling translating to C++.
closed account (3hM2Nwbp)
I KNEW that Disch was an underpants gnome, I just knew it!






( http://en.wikipedia.org/wiki/Gnomes_%28South_Park%29 )

forget I said anything if you don't know what I'm talking about

sorry for the spam
Topic archived. No new replies allowed.