# Binary to decimal question

Page 1 of 1

## 12 Replies - 688 Views - Last Post: 11 March 2013 - 06:33 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=314782&amp;s=99f4d9b99c70db76a8a4248450ae7c0b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Sandals456

Reputation: 0
• Posts: 44
• Joined: 02-February 13

# Binary to decimal question

Posted 08 March 2013 - 12:34 PM

I am creating a program that will translate a binary number with a maximum of 20 digits into a decimal number. I have some of the steps, I just need a bit of help in translating into code:

1. User will input binary.
2. The program will reverse the binary number in order to start from the last integer all the way to wherever the user finishes with the binary number.
3. It will then multiply to the power of 0, 1, 2... wherever the number 1 is present and so on until the last integer.
4. It then adds up all the numbers and print out as decimal.

Is there a way to create an unlimited size array in order to put it into the decimal.

```#include<stdio.h>

int main()
{
int binary_number[20], decimal_number[/*I can't leave it blank because it needs an integer so I don't know what to put*/], input;

printf("Please enter a binary number: ");
for(input = 0; input<20; input++)
scanf("%d", &binary_number[input]);

/*Call out decimal_number function*/

}

```

I also have to create a function in order to call it out and print in the main function.

Is This A Good Question/Topic? 0

## Replies To: Binary to decimal question

### #2 Sandals456

Reputation: 0
• Posts: 44
• Joined: 02-February 13

## Re: Binary to decimal question

Posted 08 March 2013 - 12:44 PM

The user will input the binary number as a string, so I have to convert it into an integer and then reverse the order to continue.

Isn't translating from a string to an integer

```'string'-'0'

```

or something like that?

### #3 CTphpnwb

• D.I.C Lover

Reputation: 3177
• Posts: 11,259
• Joined: 08-August 08

## Re: Binary to decimal question

Posted 08 March 2013 - 06:49 PM

You could do that but since you're dealing with 1 and 0 you could use an if ... else.
```if(binary_number[input] == '1') { use_one(); } else if(binary_number[input] == '0') { use_two(); }
```

### #4 jjl

• Engineer

Reputation: 1118
• Posts: 4,641
• Joined: 09-June 09

## Re: Binary to decimal question

Posted 08 March 2013 - 08:25 PM

Quote

Is there a way to create an unlimited size array in order to put it into the decimal.

A array resides in memory, which is hardware. You can't have unlimited hardware.

There is no reason to use an array for the decimal number in this assignment. You can keep a running total as you iterate over the reversed binary.

i.e.
```
input binary number
reverse binary number

i = 0
decimal = 0
iterate over binary number
decimal number += (binary at index i) * 2^i

```

This post has been edited by jjl: 08 March 2013 - 08:25 PM

### #5 Sandals456

Reputation: 0
• Posts: 44
• Joined: 02-February 13

## Re: Binary to decimal question

Posted 08 March 2013 - 11:48 PM

Well this is what I have so far:

```#include<stdio.h>

int main()
{
char binary_number[20];
int user_input, calculated_decimal;
int s;

printf("Please input a binary number: ");
scanf("%s", binary_number);

/*Call out decimal function into main function*/

printf("This is the binary number in a decimal number: %d", calculated_decimal);

return 0;
}

void calculated_decimal(char *temp)

{
int s;
char binary_number[20]
for(s = strlen(binary_number)-1; s >= 0; s--)
{
if(binary_number[s] == '1')
{
sum = sum + pow(2,s);
}

}
return sum;
}

```

I just don't know how to call out the calculated_decimal function into the main function.

### #6 snoopy11

• Engineering ● Software

Reputation: 927
• Posts: 2,787
• Joined: 20-March 10

## Re: Binary to decimal question

Posted 09 March 2013 - 02:04 AM

Hmm,

where to start is the problem....

Is this really an honest effort by you...?

you declare variables and dont use them...

you dont declare functions but try to use them...

you have variable names the same name as functions...

you dont seem to know how to use functions....

your function for calculating the decimal from a binary is almost right but
ultimately is wrong...

The best I can do for you at this stage is recommend you read up on functions

functions...

Jacks Function Tutorial the basics

read that and come back for more help once you have understood it..

Snoopy.

### #7 Sandals456

Reputation: 0
• Posts: 44
• Joined: 02-February 13

## Re: Binary to decimal question

Posted 09 March 2013 - 01:58 PM

Here is what I have

```#include<stdio.h>

void calculated_decimal(int user_input);
int main()
{
char binary_number[20];
int user_input, calculated_decimal;
int sum;

printf("Please input a binary number: ");
scanf("%s", &binary_number[user_input]);

calculate_decimal(binary_number);

printf("The binary number %d is %d", binary_number, calculated_decimal);

return 0;
}

void calculated_decimal(int user_input)
{

if(binary_number[user_input] == '1')
{
sum =
}

return sum;
}

```

### #8 Sandals456

Reputation: 0
• Posts: 44
• Joined: 02-February 13

## Re: Binary to decimal question

Posted 09 March 2013 - 02:33 PM

I don't know if the callout function for calculated_decimal is right.

```#include<stdio.h>

void calculated_decimal(int user_input, char binary_number[20]);

int main()
{

char binary_number[20];
int user_input, calculated_decimal;
int sum;

printf("Please input a binary number: ");

/*Would I need a for loop to scan the numbers into the binary_number array?*/
for(user_input = 0; user_input<20; user_input++)
scanf("%s", &binary_number[user_input]);

calculated_decimal(binary_number);

printf("The binary number %d is %d", binary_number, calculated_decimal);

return 0;
}

void calculated_decimal(int user_input, char binary_number[20])
{

if(binary_number[user_input] == '1')
{
sum =
}

return sum;
}

```

### #9 snoopy11

• Engineering ● Software

Reputation: 927
• Posts: 2,787
• Joined: 20-March 10

## Re: Binary to decimal question

Posted 09 March 2013 - 02:54 PM

No its not...

it should return an int

like so...

```int calculated_decimal(char *temp)

{
int s=0, sum=0;

for(s=strlen(temp);s>=0; s--)
{
int counter = (strlen(temp)-s)-1;
if(temp[s] == '1')
{
sum = sum + pow(2,counter);

}

}
return sum;
}

```

and called like

calculated_decimal(binary_number);

Snoopy.

### #10 Sandals456

Reputation: 0
• Posts: 44
• Joined: 02-February 13

## Re: Binary to decimal question

Posted 09 March 2013 - 03:15 PM

snoopy11, on 09 March 2013 - 02:54 PM, said:

No its not...

it should return an int

like so...

```int calculated_decimal(char *temp)

{
int s=0, sum=0;

for(s=strlen(temp);s>=0; s--)
{
int counter = (strlen(temp)-s)-1;
if(temp[s] == '1')
{
sum = sum + pow(2,counter);

}

}
return sum;
}

```

and called like

calculated_decimal(binary_number);

Snoopy.

I did read it. I just thought the void would prevent the callout function from showing up when the program was compiled because the program has to print out through the main function, not the callout function.

I meant the calling of the function in line 18; I don't know if it is right.

I am getting an error: "warning: incompatible implicit declaration of built-in function strlen" and the same for the "pow" function.

### #11 snoopy11

• Engineering ● Software

Reputation: 927
• Posts: 2,787
• Joined: 20-March 10

## Re: Binary to decimal question

Posted 10 March 2013 - 12:36 AM

Sandals456, on 09 March 2013 - 10:15 PM, said:

I meant the calling of the function in line 18; I don't know if it is right.

I am getting an error: "warning: incompatible implicit declaration of built-in function strlen" and the same for the "pow" function.

The calling of the function in line 18 is incorrect.

You forgot to include certain headers for the warnings.

Regards

Snoopy.

### #12 Sandals456

Reputation: 0
• Posts: 44
• Joined: 02-February 13

## Re: Binary to decimal question

Posted 10 March 2013 - 07:11 PM

Here is what I have, I don't know how to call the function correctly. When I run it, the result is always 0.

```#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char BinaryInput; int dec = 0;

printf("Please enter a binary string: ");
while (BinaryInput != '\n')
{
scanf("%c",&BinaryInput);
}
BintoDec(BinaryInput);

printf("Decimal Representation is: %d", dec);
return 0;

}
BintoDec(int dec)
{
int BinaryInput;

if (BinaryInput == '1')
dec = dec *2 + 1;
else if (BinaryInput == '0')
dec *=2;

return 0;
}

```

This post has been edited by Sandals456: 10 March 2013 - 07:12 PM

### #13 snoopy11

• Engineering ● Software

Reputation: 927
• Posts: 2,787
• Joined: 20-March 10

## Re: Binary to decimal question

Posted 11 March 2013 - 06:33 PM

That is further away than the original post..

Take a deep breath....

I gave you a working function..
```int calculated_decimal(char *temp)

{
int s=0, sum=0;

for(s=strlen(temp);s>=0; s--)
{
int counter = (strlen(temp)-s)-1;
if(temp[s] == '1')
{
sum = sum + pow(2,counter);

}

}
return sum;
}

```

all you had to do was call it correctly

printf("This is the binary number in a decimal number: %d", calculated_decimal(binary_number));

You need to include the headers
math.h for pow
and
string.h for strlen

Regards

Snoopy.