Binary to decimal conversion

This is a discussion on Binary to decimal conversion within the C Programming forums, part of the General Programming Boards category; I've been working on this for awhile now and I've decided to start over.
I'm trying to make a program ...

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.

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.

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.

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

That code should cause your compiler to vomit warnings and errors all over the place.

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.

You don't need nested loops. Just one loop after you get the input is fine.

A double == is for comparison, a single = is for assignment. pointer == binary doesn't do anything, but you don't even need that.

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.

^ 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.

You're not actually accumulating the values in decimal, you just overwrite. You need to add values to decimal.

_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

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.