Thread: Binary to decimal conversion

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    20

    Binary to decimal conversion

    I've been working on this for awhile now and I've decided to start over.
    I'm trying to make a program where a user can enter 8 binary digits and have it output a number in decimal equal or less than 255. So as to not make the people weary of me here I've been on other forums and haven't had too much luck.
    My idea is simple but I don't know how to translate it in c in my head.
    First the user enters in a series of binary numbers and the program multiplies 128, 64, 32 by what ever digit corresponds to it. Then the program adds all the numbers up. Sounds simple I know! What I don't know is how to do my inputs.
    Code:
    #include <stdio.h>
    #include "conio.h"
    
    int main()
    
    {
    	int binary;
    	int a,b,c,d,e,f,g,h;
    	int pointer;
    	int decimal;
    
    	printf("Please enter in a b it binary number.");
    	scanf("%d);
    	for (i=0, i < 8,  i++ )
    	{
    		scanf("%d,i);
    
    		
    		printf("Your value in decimal is "%c".");
    It's kinda a mess. What I'm trying to do is have i represent each of the 8 bit digits and have a for statement increment i down the row of binary digits. Is their a simple way? I'm asking because I know the more experienced people can cut down on my programming time.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    That isn't a mess, mostly because there isn't really anything there.

    This will be easiest if you get the input as an array of chars (remember to leave room for 8 digits plus the null terminator). You'll want to use scanf with the %s modifier, or use fgets for this. Then, move along the array, if you see a '1' (ASCII '1', not numeric 1), multiply by the right power of two and add it to your accumulator/total variable. If it's a '0', don't add anything. Give that a shot, and post back if you have trouble.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't actually have anything called i in your code. Also, scanf expects a pointer, so if you did have i, you would need &i there.

    You might consider using an array instead of a b c d e f g h. You could also look at the bitwise operator FAQ we have here.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    20
    Code:
    #include <stdio.h>
    #include "conio.h"
    
    int main()
    
    {
    	
    	char binary [128];
    	int pointer;
    	int exponent;
    	int decimal;
    	int storage_variable;
    	printf("Please enter in an 8 bit binary number.");
    	fgets("%c", &binary);
    	for (pointer=0, pointer < 8,  pointer++ )
    	{
    		pointer == binary; 
    		
    		for (exponent=7, exponent > 0, exponent--)
    		{
    			storage_variable= binary * 2^exponent;
    		
    			decimal= storage_variable;
    			printf("Your value in decimal is %c", decimal);
    					
    		}
    		
    		  
    		
    	}
    		
    	}
    
    	_getch();
    	return 0;
    }

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    20
    I'm kinda stuck again The pointer is pointing down the row of binary numbers. The binary number is multiplied by the power of exponent from 7 to 0 and the answer is stored in storage_variable. The decimal value should be the sum of the storage variables...

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    for (exponent=7, exponent > 0, exponent--)
    You likely mean this
    Code:
    for (exponent=7; exponent > 0; exponent--)
    And this is not what you want; "^" is an bit-wise operator not the power operation.
    Code:
    2^exponent
    http://www.cplusplus.com/reference/clibrary/cmath/pow/

    please read the following for other mistakes you made:
    http://www.cplusplus.com/reference/c...cstdio/printf/
    http://www.cplusplus.com/reference/c.../cstdio/fgets/

    Note: After all the above there is still more problem with your code; I think you have about 1 error per 2 lines of code in your short program.

    Tim S.
    Last edited by stahta01; 06-17-2011 at 06:03 PM.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    That code should cause your compiler to vomit warnings and errors all over the place.
    1. Read the docs on how to use fgets. It's something like fgets(where to put it, how much space I have to put it, where to get it from). You're mixing scanf and fgets, and still using the wrong modifier.
    2. You don't need nested loops. Just one loop after you get the input is fine.
    3. A double == is for comparison, a single = is for assignment. pointer == binary doesn't do anything, but you don't even need that.
    4. binary is an array of characters. Using it's name, like you did in storage_variable= binary * 2^exponent; gives you the address of the first element of the array, not magically the character at theposition you want.
    5. ^ is the bitwise OR operator, not exponentiation. You would need to use the pow() function (math.h) for that, but there's a better way. Use a variable that tracks the current power of 2.
    6. You're not actually accumulating the values in decimal, you just overwrite. You need to add values to decimal.
    7. _getch() is non-standard. Ditch it and use getchar() instead.


    Here's a little pseudo code to get you going:
    Code:
    get input from user
    initialize decimal to 0
    initialize exponent to 1
    for i from index of last element in input, down to 0  // use strlen to figure out the index of the last element, but remember, arrays start at 0 in C, not at 1
        if binary[i] is a '1'
            add exponent to decimal
        increase exponent to next power of 2

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Stop! You're applying the 1 million monkeys method of programming (you give a million monkeys a million typewriters and eventually one of them will write something useful). Like those 1 million monkeys, you'll produce just as much garbage getting there.

    Concentrate on the bits that you think you can do first no mater how small they are, get them correct, then move on to the bits you aren't as sure on. Don't try and run before you can walk.
    Don't try nested loops before you've got a single loop to work.
    Don't try calculating the correct result until you've learned what the operators themselves do.
    Don't tackle the algorithm without even having compiled what you have.

    I know you feel overwhelmed by your lack of knowledge here, but there is NO shortcut to learn to program. You can't skip the learning and be instantly capable of producing the final result. The fastest way to get there is to slow down and take the time to learn about the stuff you are working on. It will not take long to learn what you need to for this if you go about that learning the right way. Otherwise even if you get a working program, it will look exactly like a program that was written by someone who did not know what they were doing. I.e. you might know how to actually paint, but you wont be producing the mona lisa.
    Last edited by iMalc; 06-17-2011 at 09:38 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by anduril462 View Post
    ^ is the bitwise OR operator...
    An 'X' slipped through your fingers there!
    Devoted my life to programming...

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Sipher View Post
    An 'X' slipped through your fingers there!
    Don't you just hate it when your keyboard drops characters like that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. decimal to binary conversion
    By roelof in forum C Programming
    Replies: 41
    Last Post: 05-16-2011, 03:32 AM
  2. Decimal to Binary conversion help
    By tru.cutru in forum C Programming
    Replies: 3
    Last Post: 07-08-2008, 10:17 PM
  3. binary decimal conversion
    By eerok in forum C Programming
    Replies: 2
    Last Post: 01-24-2006, 09:51 PM
  4. Decimal to binary conversion
    By blckspder in forum C Programming
    Replies: 5
    Last Post: 04-07-2005, 12:38 PM
  5. decimal to binary conversion
    By noob2c in forum C Programming
    Replies: 4
    Last Post: 05-29-2003, 08:07 PM