# Thread: Binary to Decimal-- Need help

1. ## Binary to Decimal-- Need help

Ok, so I am trying to create a program that takes as an input a binary number, 1-10 digits. My program will take this input, which will have no spaces between the 1's and 0's and return a decimal number. My current program, which is appended, fails my test to check whether or not the numbers are 1's and 0's. Anyone have any ideas as to where I have logic errors? Also, I get the error:
"In function 'conv'
105:warning: too many arguements for format"

It still compiles, but I dont know what this means.

Anyway, here it is, any tips and pointers would be appreciated. Also, if there is a better way to approach this problem, let me know.

Code:
```#include <stdio.h>
#include <math.h>
#include <stdlib.h>

enum Status {VALID, NONVALID};

int conv(int x);

int main()
{

int binary,temp,temp2,i,orig;
enum Status bin=NONVALID;

while(NONVALID==bin)
{
printf("Enter a binary number(1-10 digits):");
scanf("%d",&binary);

temp=binary=orig;

for(i=1;i<11;i++)
{
temp2=temp%10;
if(temp2==0||temp2==1)
{
temp=floor(temp/10);
}
else
{
break;
}
}

if(i==11)
{
bin=VALID;
}
else
{
bin=NONVALID;
printf("Sorry: each digit must be a 1 or a 0!");
}
}

conv(binary);

return 0;
}

int conv(int x)
{
int decval=0;
int temp,j,orig,temp2,binary;

for(j=1;j<=10;j++)
{
if(1==temp)
{
switch(j)
{

case 1:
decval+=1;
break;

case 2:
decval+=2;
break;

case 3:
decval+=4;
break;

case 4:
decval+=8;
break;

case 5:
decval+=16;
break;

case 6:
decval+=32;
break;

case 7:
decval+=64;
break;

case 8:
decval+=128;
break;

case 9:
decval+=256;
break;

case 10:
decval+=512;
break;
}
}
binary=floor(binary/10);
}
printf("%%d in binary is %d in decimal",orig,decval);

return 0;
}```
Thanks again in advance,

Evan

2. Hello evandb,

I know of a faster way to convert binary to integer.

Code:
```int btoi(char *buf) {
int count, total, i, j, tmp;

total = 0;
count = strlen(buf);

for (i = 0; i <= count; i++) {
if (buf[count-i] == '1') {
tmp = 1;
for (j = 1; j < i; j++)
tmp *= 2;
total += tmp;
}
}

return total;
}```
Code 1.1: Binary to Integer

This code will insert binary, e.g. 00000000000000000000000001111011 and convert it to 123.

If you want to convert an integer to binary you could do something like:

Code:
```char* itob(int n) {
static char temp[32];
char *buf = &temp[0];
pos = 0;

for (i = 31; i >= 0; i--) {
buf[pos] = "01"[((n >> i) & 1)];
pos++;
}
buf[pos] = '\0';

return buf;
}```
Code 1.2: Integer to Binary

Though these are example codes, they work quite well for me. Here is a sample of how to use them in a program:

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

int main() {
int n;
char input[255];

printf("Enter a signed integer: ");
fgets(input, sizeof(input), stdin);

n = atoi(input);	// convert string to integer
printf("Binary value of %s is %s\n", input, itob(n));

return 0;
}```
Example 1.1: Using itob()

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

int main() {
char input[255];

printf("Enter a binary (integer) value: ");
fgets(input, sizeof(input), stdin);

printf("Integer value of %s is %i\n", input, btoi(input));

return 0;
}```
Example 1.2: Using btoi()

The code is alot smaller and more effective. I hope the helps,
- Stack Overflow

3. What's the point of just giving them their entire homework solution Stack Overflow? What do they really learn from it? You could always read the forum guidelines, in which you'd see something along the lines of "don't ask people to do your work for you". There should be a converse to that: "Don't do all of their work for them."

To answer your question:
Code:
`printf("%%d in binary is %d in decimal",orig,decval);`
The format specifier %% outputs one % sign. As such, the d after it is just treated as if it were the letter d, and is written to your stdout. Then, the next %d lines up with orig, leaving the extra parameter decval on its own with no format specifier to match it up to.

Quzah.

4. >temp=binary=orig;

This assigns temp and binary to garbage. orig has not been initialized, and you are assigning binary and temp the value of orig. Try either:
temp=orig=binary;

Or since you don't use orig:
temp=binary;

5. Originally Posted by Stack Overflow
Hello evandb,

I know of a faster way to convert binary to integer.

Code:
```int btoi(char *buf) {
int count, total, i, j, tmp;

total = 0;
count = strlen(buf);

for (i = 0; i <= count; i++) {
if (buf[count-i] == '1') {
tmp = 1;
for (j = 1; j < i; j++)
tmp *= 2;
total += tmp;
}
}

return total;
}```
Code 1.1: Binary to Integer

This code will insert binary, e.g. 00000000000000000000000001111011 and convert it to 123.

If you want to convert an integer to binary you could do something like:

Code:
```char* itob(int n) {
static char temp[32];
char *buf = &temp[0];
pos = 0;

for (i = 31; i >= 0; i--) {
buf[pos] = "01"[((n >> i) & 1)];
pos++;
}
buf[pos] = '\0';

return buf;
}```
Code 1.2: Integer to Binary

Though these are example codes, they work quite well for me. Here is a sample of how to use them in a program:

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

int main() {
int n;
char input[255];

printf("Enter a signed integer: ");
fgets(input, sizeof(input), stdin);

n = atoi(input);	// convert string to integer
printf("Binary value of %s is %s\n", input, itob(n));

return 0;
}```
Example 1.1: Using itob()

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

int main() {
char input[255];

printf("Enter a binary (integer) value: ");
fgets(input, sizeof(input), stdin);

printf("Integer value of %s is %i\n", input, btoi(input));

return 0;
}```
Example 1.2: Using btoi()

The code is alot smaller and more effective. I hope the helps,
- Stack Overflow
Q.1 what is the algorithm of this conversion (itob) and what is the meaning of "01"[....] of
buf[pos] = "01"[((n >> i) & 1)]; ?

Q.2 I fail to convert signed integer to 48-bit binary using this algorithm by changing "static char temp[32];" to "static char temp[48];" and "for (i=47;.................) , what's wrong with it?

Thank you very much

6. A.1 - Given the string "01", give us position (n shifted right i places) AND 1. Thus, if the bit is not set, we use the 0 position of the string, if it is, then we use the 1.
A.2 - Does your compiler have 48 bit integers? I doubt it. That's why it fails.

Quzah.

Popular pages Recent additions