# Write a program to convert a decimal number into any base from 2 to 16

Page 1 of 1

## 2 Replies - 5442 Views - Last Post: 03 April 2012 - 11:26 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=273582&amp;s=1ccbbc296e2603cef14bf47a0de81577&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 pop0008

Reputation: 0
• Posts: 1
• Joined: 02-April 12

# Write a program to convert a decimal number into any base from 2 to 16

Posted 02 April 2012 - 06:20 PM

Hey there, I'm completely new to c++ programming, my first uniassignment is to write a program to convert a decimal into any base from 2 to 16, with full error checking required. I am not asking for someone to write me a code, just to briefly explain what this actually means (converting decimal to base?? also what the hell is error checking?)

KD
Is This A Good Question/Topic? 0

## Replies To: Write a program to convert a decimal number into any base from 2 to 16

### #2 r.stiltskin

• D.I.C Lover

Reputation: 1833
• Posts: 4,927
• Joined: 27-December 05

## Re: Write a program to convert a decimal number into any base from 2 to 16

Posted 02 April 2012 - 06:31 PM

The first lesson you should take away from this is that you can (and should) look up almost anything in Google first, since you will usually find a large selection of more or less comprehesive answers to your question there -- more thorough than what anyone here can type out in a minute or two.

After that, if you find that you don't understand what you're finding there, then it makes sense to ask specific questions about what's puzzling you.

I suggest you start by reading this tutorial (which I found by googling "numerical bases").

(And regarding error checking I assume your teacher wants you to write code that will verify that the user's inputs are "valid numbers", but you should probably consult your class notes in case something else was discussed in class.)

This post has been edited by r.stiltskin: 02 April 2012 - 06:34 PM

### #3 buffalobill

Reputation: 21
• Posts: 188
• Joined: 08-July 08

## Re: Write a program to convert a decimal number into any base from 2 to 16

Posted 03 April 2012 - 11:26 PM

The following shows the principle of converting from base 10 to some other radix in this case base 2. To handle the 14 bases it may be convenient to use the switch construct with each case dedicated to the conversion method for one base.

Converting from decimal (base 10) to another base.
Convert 1234510 to base2.
2|12345 r 1
2| 6172 r 0
2| 3086 r 0
2| 1543 r 1
2| 771 r 1
2| 385 r 1
2| 192 r 0
2| 96 r 0
2| 48 r 0
2| 24 r 0
2| 12 r 0
2| 6 r 0
2| 3 r 1
2| 1 r 1
2| 0 read the binary from here upward
So decimal 12345 = binary 11,0000,0011,1001
checking 8192+4096+0+48+9=12345
edit: sorry for the column alignment problem

This post has been edited by buffalobill: 03 April 2012 - 11:28 PM