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

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

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

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.

7. 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'
increase exponent to next power of 2

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

9. Originally Posted by anduril462
^ is the bitwise OR operator...
An 'X' slipped through your fingers there!

10. Originally Posted by Sipher
An 'X' slipped through your fingers there!
Don't you just hate it when your keyboard drops characters like that