Hi there ..

i need a source code for a program written in C++ to convert a number in base b to a number in base c, while both b and c can be equal to either 2, 8, 10, or 16.

i'll really appreciate it if anyone can help

& thanX :)

Printable View

- 12-09-2006Noraa C++ source code..
Hi there ..

i need a source code for a program written in C++ to convert a number in base b to a number in base c, while both b and c can be equal to either 2, 8, 10, or 16.

i'll really appreciate it if anyone can help

& thanX :) - 12-09-2006vart
itoa(i,buffer,base)

- 12-09-2006strikerQuote:

Originally Posted by**Nora**

- 12-09-2006swghQuote:

Here i have attached the complete code for the same..Enjoy

for them. It really does go against the learning process - 12-09-2006strikerQuote:

Originally Posted by**swgh**

- 12-09-2006dwks
itoa (ugh), strtol or *scanf or *printf (old), or stringstreams would work. Even regular old math would work.

- 12-10-2006Nora
Hi again..

i'm just a begginer in C++ .. and i have the problem i mentioned before as a project..

i've tryed to solve it .. and i have some questions..

is it right if do the following:

cout>>"Please enter the number you want to convert followed by its base";

cin<<num,base;

then i put an "if"statement .. if the base is not equal to 10 then it will enter a "for" loop to convert it it to a decimal number..then another loop to convert this decimal to the base that the user choose after that..

this is for a start is it right or i have to think about something else? cuz i need to know so that i can go on with the rest of the code..

that's it.. thank you all for ur replys :) - 12-10-2006Salem
Well you start with a very simple program which

- prompts for input (you've had a go at that)

- reads input (you've had a go at that as well)

- prints what was input.

When that's working, move onto the next small step of the program, or if you're still stuck, post the code you have so far. - 12-16-2006Nora
Hi all ...

i getting really tired by this source code! i have asked our TA about it.. and he said u need to use strings in order to make it easier for you .. but we didn't learn strings at all!

so its becaming really complicated :s i got the source where i convert from decimal to any base .. i just need the source that convert from any base to decimal... any help from anyone would be really helpful cuz i'm really desperate :/ - 12-16-2006hardiQuote:

Originally Posted by**Nora**

Code:`Base 2(binary) -> Base 10(decimal)`

100101 -> 1*2^0 + 0*2^1 + 1*2^2 + 0*2^3 + 0*2^4 + 1*2^5 = 1 + 0 + 4 + 0 + 0 + 32 = 37

Base 8(oct) -> Base 10(decimal)

720123 -> 3*8^0 + 2*8^1 + 1*8^2 + 0*8^3 + 2*8^4 + 7*8^5 = 237 651

Generally. By converting from base X to base 10, you have to use this technique

Let's say A is an array (0-based index - meaning that the array's indexes are 0 1 2 3 ...)

A = { 1, 0, 1, 0, 0, 1 } //The numbers from the 1st example - except they are written backwards,

so as not to mess up those indexes. You have to read the indexes from right to left and then number

them 0 1 2 3 and so on, but in C and C++, arrays start with index 0, thus if you would write the numbers

in the same order that you see them in the 1st example, the indexes would be reversed.

then you have to add them together using this formula:

A[i]*X^i + A[i + 1]*X^(i + 1) .. A[the number of elements in the array A - 1]*X^(the number of elements in the array A - 1)

If you still don't get it, use Google!