Thread: binary to decimal

  1. #1
    Registered User
    Join Date
    Mar 2013
    Location
    south africa
    Posts
    7

    binary to decimal

    hi this is my code. The code should convert binary numbers to decimal but it doesn't...can any1 please check where i could have gone wrong.

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    int main()
    {
         
        char s[40];
        int base;
        int index,n,p,sum=0;     /*n is the number of digits in the converted value*/
        
        printf("enter the number and base: ");
        scanf("%s %d",s,&base);
        
        for(n=strlen(s)-1;n>=0;n--)
        {
            p=strlen(s);
            for(index=strlen(s)-(p-1); index<=p; index++)
            sum += s[index] * pow(base,n);
        }
        printf("%d",sum);
        printf("\n");
    
    }
    Last edited by mast3124mind; 03-27-2013 at 06:10 PM.

  2. #2
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    If you are looking to convert only binary numbers into only the decimal format, why are you asking the user for a base? For the sake of simplicity, remove the user input factor and just hard-code numbers into a string and focus on converting from binary to decimal.

    Adjust your code and re-post.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Why do you have two for loops?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    Mar 2013
    Location
    south africa
    Posts
    7
    There you go man...

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    int main()
    {
          
        char s[40];
        int base=2;
    
        int index,n,p,sum=0;     /*n is the number of digits in the converted value*/
         
        printf("enter your binary number:");
    
        scanf("%s",s);
    
         
        for(n=strlen(s)-1;n>=0;n--)
        {
            p=strlen(s);
            for(index=strlen(s)-(p-1); index<=p; index++)
            sum += s[index] * pow(base,n);
        }
        printf("%d",sum);
        printf("\n");
     
    }

  5. #5
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Logic would be, declare a counter and initialize to zero, then start at the end of the string. For every bit, multiply by "2 raised to the counter variable" and add to sum( are you sure you're currently raising to the right power? ), then increment the counter.

    As stahta01 said, you should only need one loop to accomplish this.

    One more thing: you are calling strlen multiple times on the same string when you only need to call it once and save it to a variable.

    There YOU​ go...............man.
    Last edited by jwroblewski44; 03-28-2013 at 02:48 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Decimal to Binary
    By johngoodman in forum C Programming
    Replies: 6
    Last Post: 02-27-2013, 12:28 AM
  2. binary to decimal
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 03-14-2004, 08:35 PM
  3. binary to decimal ! how?
    By o0o in forum C++ Programming
    Replies: 8
    Last Post: 12-23-2003, 10:31 PM
  4. binary to decimal
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 02:47 PM
  5. decimal to binary, decimal to hexadecimal and vice versa
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2001, 11:07 PM