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

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




decimal to any base conversion

 
Reply to this topicStart new topic

decimal to any base conversion

rockyroad
post 13 Oct, 2006 - 06:30 AM
Post #1


New D.I.C Head

*
Joined: 13 Oct, 2006
Posts: 2


My Contributions


I am having trouble finding/making a source code about decimal to any base conversion in c++. Do you guys have some idea on this? Thanks:blink:
User is offlineProfile CardPM

Go to the top of the page


William_Wilson
post 13 Oct, 2006 - 06:45 AM
Post #2


lost in compilation

Group Icon
Joined: 23 Dec, 2005
Posts: 3,951



Thanked 13 times

Dream Kudos: 3275

Expert In: Java, C, Javascript

My Contributions


It is a simple algorithm, but without any attempt at code i will not be writting it for you, but i will explain it:
you simply divide the starting number by you base and the remainder is what will be displayed, and the whole part is what you will use to find the next number to display, example:
17 to base 2 (binary)

QUOTE

17 / 2 = 8 R 1
8 / 2 = 4 R 0
4 / 2 = 2 R 0
2 / 2 = 1 R 0 (since we have 1 left)
R 1

now you read this upwards: 10001 is the equivalent, but this works for any base:
17 base 3:
17 / 3 = 5 R 2
5 / 3 = 1 R 2
R 1
there for 17 in base 3 is: 122

once the final division value is less than the base value, you are finished and the division value is the first # in the newly converted base.

*Note that not all bases convert nicely, even numbers definatly work a lot more consistantly than odd numbers.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/20/08 03:46AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month