# Need help for binary to decimal code

Page 1 of 1

## 3 Replies - 1120 Views - Last Post: 03 September 2009 - 05:12 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=123695&amp;s=8c15607d1b187cd581055ce6e0df9496&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 tsmith118

Reputation: 0
• Posts: 13
• Joined: 03-September 09

# Need help for binary to decimal code

Posted 03 September 2009 - 02:18 AM

I am new to programming. I am in my 1st course and I took it because there were no pre-requisites. After getting in the class, I learn that you need some programming knowledge. I am on my first assignment and struggling. I was told about this site, and that I could possibly get help here. Let me start by saying I am not just looking for a code I can copy and paste, I need to learn how to do this stuff. I am just looking for help getting pointed in the right direction. I have a code started, I just need to change it so that it will figure the decimal number for a binary number, not just add the "1's" of a binary number together. Any help would be appreciated. I am using DevC++. It has to be configured to accept a 16 digit binary number and also set up to account for a user entering less than the required 16 digits, i.e 1001001 needs to be calculated as 0000000001001001. I have C++ for dummies that I bought to try to help me get this stuff, but I am not far enough into that yet to be able to do this problem. The text book is still waiting to be delivered. Also, I have looked at all 23 pages of the snippets. There is 1 that matches my problem, but being new to this I am struggling trying to decipher what is said there to what I need to do in my program. I am trying, not here for a quick answer. Below is what I have to work with. Can anyone help??

```#include <iostream>
#include <math.h>

using namespace std;

int main(void)

{

char b[16];

cout << "Please input a binary number up to 16 digits...\n\n";

cin >> b;

int sum = 0, i=0;

while (i<16 && b[i] != (char)0) {

if (b[i] == '1') sum++;

else if (b[i] == '0') ;

else cout << " you are not inputting a binary number! \n";

i++;

}

cout << sum;

char c; cin >> c;

return 0;

}

```

*** MOD EDIT: Fixed code tags. Please ***

This post has been edited by JackOfAllTrades: 03 September 2009 - 04:29 AM

Is This A Good Question/Topic? 0

## Replies To: Need help for binary to decimal code

### #2 learnplaycreate

Reputation: 7
• Posts: 119
• Joined: 27-August 09

## Re: Need help for binary to decimal code

Posted 03 September 2009 - 02:49 AM

hey man,
not sure how much help i can be only being learning this stuff for a couple of weeks i found an excellent tutorial at http://www.cplusplus.com/doc/tutorial/ that should be able to help you while your waiting for your book to arrive.
i've written some code to do exactly what you want, i'm just not sure it's the best way to do it, as you said you didn't wat the code, you'll need to start by putting the binary input into an array or string, and using a loop to read it from the last element to the first, i've included below the loop i used for the same problem, if you don't understand loops you'll need to read the tutorial i've recommended or a similar one thats available on this site, i'm sure there is one, the people on this site are v knowledgeable and have helped me heaps in the last two
```for(;x>1;x--)
{

cout << bi[p];
p++;
n = n*2;

if (bi[x-2] == '1')
{
z = z+n;
}

}

```

x, is generated by finding the array length srtlen() would be a helpful function to read up on, also this loop deliberately does not take any notice of the first binary digit, as it was causing some problems with n*2 so you'll need to add something after the loop to account for the value of the first binary number, bi[] is the array that the binary number was entered into. and i'm not sure i'm right at all as i said i've only been playing with code for two weeks.
weeks.

### #3 tsmith118

Reputation: 0
• Posts: 13
• Joined: 03-September 09

## Re: Need help for binary to decimal code

Posted 03 September 2009 - 03:52 AM

Thanks, right now anything is a step forward. I will see what I can do with the additional information provided. Good Luck to me. I have 34 hours to get this figured out.

### #4 learnplaycreate

Reputation: 7
• Posts: 119
• Joined: 27-August 09

## Re: Need help for binary to decimal code

Posted 03 September 2009 - 05:12 AM

tsmith118, on 3 Sep, 2009 - 02:52 AM, said:

Thanks, right now anything is a step forward. I will see what I can do with the additional information provided. Good Luck to me. I have 34 hours to get this figured out.

write this on a piece of paper and keep it in mind when writing your code.

...........1024,256,128,64,32,16,8,4,2,1

this is the basic table used to convert binary to dec manualy now you just ask the computer to do what you you would do either in your head or on a piece of paper.
good luck