Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,111 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,743 people online right now. Registration is fast and FREE... Join Now!




number base converter in C++

 
Reply to this topicStart new topic

number base converter in C++, ASCII and C++

devilchild
17 Apr, 2007 - 12:49 PM
Post #1

New D.I.C Head
*

Joined: 17 Apr, 2007
Posts: 42


My Contributions
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.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Number Base Converter In C++
17 Apr, 2007 - 03:47 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Number Base Converter In C++
18 Apr, 2007 - 07:46 AM
Post #3

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,858



Thanked: 49 times
Dream Kudos: 550
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:42PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month