# number base converter in C++

Page 1 of 1

## 2 Replies - 4131 Views - Last Post: 18 April 2007 - 08:46 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=26733&amp;s=3087436195b6ad1153c026f3aa96dd58&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 devilchild

Reputation: 0
• Posts: 42
• Joined: 17-April 07

# number base converter in C++

Posted 17 April 2007 - 01:49 PM

hello to everyone am new here and to programming. I have an assignment and don't understand how to get started due to the fact i don't understand C++ fully.

I have no codes to show cause i don't know how to get started as yet. So all am asking is for a guideline to how to get started and aid in helping me do my assignment.

My assignment is to convert decmial number into any other bases.

I really need the help so am asking kinda for it please.

Is This A Good Question/Topic? 0

## Replies To: number base converter in C++

### #2 NickDMax

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

## Re: number base converter in C++

Posted 17 April 2007 - 04:47 PM

THis is a popular topic as it is a popular assignment. There are several code snippets under both C and C++ that you can look at.

The basic idea is to use the idea of a positional number system:
(not that I will be using the carrot ^ to indicate an exponent, in C/C++ the carrot means XOR, you would use pow() to calculate powers in C/C++)

12343 = 1*10^4 + 2*10^3 + 3*10^2 + 4*10^1 + 3*10^0

so to convert the digits 1, 2, 3, 4, 3 into an integer in the computer you would loop though starting with 1, and add each digit, then multiply by 10 and add the next digit. etc.

0*10 + 1 = 1
1*10 + 2 = 12
12*10 + 3 = 123
123*10 + 4 = 1234
1234*10 + 3 = 12343

Once you have the number in the computer as an integer, you can use the same process in reverse to convert it into any base.

you find the remainder of the number divided by the base, and that is you Least significant digit. Then you divide the number by the base (ignore the remainder) and start over

123 % 2 = 1, 123/2=61
61 % 2 = 1, 61/2 = 30
30 % 2 = 0, 30/2 = 15
15% 2 = 1, 15/2 = 7
7 % 2 = 1, 7/2 = 3
3 % 2 = 1, 3/2 = 1
1 % 2 = 1

so the number is 1111011 in binary and we can check this by:

0 * 2 + 1 = 1
1 * 2 + 1 = 3
3 * 2 + 1 = 7
7 * 2 + 1 = 15
15 * 2 + 0 = 30
30 * 2 + 1 = 61
61 * 2 + 1 = 123

Now you try it in code and show us what you get.

### #3 NickDMax

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

## Re: number base converter in C++

Posted 18 April 2007 - 08:46 AM

Quote

thanks for your reply on my problem so far i still can't get around to put any thing down in writing cause i don't know how to used a function to convert the number. so am think can you help me out like in break down the explanation so that i could understand. ... i really did [not] pick up on what you said in your reply.

As I said before this is a very popular topic: If you go to the main listing of C/C++ topics you will see a search box at the bottom. If you type "base conversion" in there and hit the search button you will see a list of others who have also had a problems with this program. The idea of a support forum is that other might learn from the questions you have, and you might learn from the questions others have.

This recent topic has some example code
More examples

and there are many others going back a number of years.

There are also a number of snippets.

Euclid told king Ptolomy, "there is no royal road to geometry." And there is no royal road to programming, you have to sit down and work it out.

think about how numbers are written. think about how you might reprent that in your program. See if you can do the conversion on paper, and then think about the steps required. Then see if you can tell the computer to follow your steps.

One way to think about programming is as teaching. You need to teach the computer how to do someting, but no teacher can really teach what he/she does not understand.