Thread: Binary to Decimal-- Need help

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    11

    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. #2

    Post

    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
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >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. #5
    Registered User
    Join Date
    May 2004
    Posts
    1
    Quote 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
    Last edited by ting08427; 05-18-2004 at 10:05 AM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help with decimal to binary Algorithm
    By webznz in forum C Programming
    Replies: 4
    Last Post: 03-13-2008, 03:52 AM
  2. Confused by expression.
    By Hulag in forum C Programming
    Replies: 3
    Last Post: 04-07-2005, 07:52 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. binary to decimal
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 03-14-2004, 08:35 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM