# C | Help Converting Binary to Decimal



## r3drock3t88 (Jan 13, 2007)

Hey all,

I am struggling with figuring out the source code on how to convert binary to decimal.

I know that you need to take each digit in the binary number and raise it to the base and power of however many digits there are, like so...

1011 = 1x 2^3 + 0x 2^2 + 1x 2^1 + 1x 2^0 ... which would give the decimal number of 1011.

Could someone please help me figure out how to get this to work in C?

Thank you all in advance...


----------



## lotuseclat79 (Sep 12, 2003)

r3drock3t88 said:


> Hey all,
> 
> I am struggling with figuring out the source code on how to convert binary to decimal.
> 
> ...


Hi r3drock3t88,

1011 in binary notation is B in hexadecimal or 11 decimal.

-- Tom


----------



## r3drock3t88 (Jan 13, 2007)

Hi,

Was there relevance in that response that I'm missing?

Thanks.


----------



## burnthepc (Aug 12, 2007)

r3drock3t88 how are you planning to store the binary number in your code?
As 1011 is 11 decimal - if you store 11 in a standard int it will contain 1011 (basicly).

eg. 

```
{
int x = 11; // <- I've stored 11 in the memory - It's stored as 1011
cout << x; // I've output 11 to the screen - I've converted the memory 1011 to decimal 11
}
```
But I don't think that's what you're asking

I think, if I was going to go about something like that I'd store the binary digits into an array [1][0][1][1].

Then you could do something like the maths you've done above to convert from the array to decimal. That should give you enough to work on to get the code running.


----------



## DoubleHelix (Dec 10, 2004)

It looks like you had Java last semester and now are taking C programming. You're not doing yourself (and your future career) any favors by asking people for help with your code. At the very least, start writing something, then post it, and ask questions. You need to at least be able to start.


----------



## burnthepc (Aug 12, 2007)

Yeah, homework questions are pretty easy to spot. That's why I pointed you towards the right area for you to learn how to do it.

If you plan to do any coding in the future I'd try and learn as much as you can. Skipping out understanding the language will only mean years trying to "get it" on your own without the teachers...or being fired.


----------



## lotuseclat79 (Sep 12, 2003)

r3drock3t88 said:


> Hi,
> 
> Was there relevance in that response that I'm missing?
> 
> Thanks.


Hi r3rock3t88,

The relevance was your misunderstanding in your original statement - i.e. your binary formula was correct, but your conclusion was not correct:

1011 = 1x 2^3 + 0x 2^2 + 1x 2^1 + 1x 2^0 ... which would give the decimal number of 1011.

The decimal number 1011 is one thousand eleven or in binary 1111110011. Your error
was precisely that 1011 in binary is not the decimal number 1011, but decimal 11 or B in hex notation.

binary=decimal=hex
0000=0
0001=1
0010=2
0011=3
0100=4
0101=5
0110=6
0111=7
1000=8
1001=9
1010=10=A
1011=11=B
1100=12=C
1101=13=D
1110=14=E
1111=15=F
10000=16=10
10001=17=11
10010=18=12
10011=19=13
...

-- Tom


----------



## -Fabez- (Jul 28, 2008)

I get what you want  You want to convert a binary number like 100 into its decimal equivilent 4. I found this link as it explains it, if you get stuck I will be happy to help


----------



## peter03 (Sep 18, 2008)

1. Get the last digit of the hex number, call this digit the currentDigit. 
2. Make a variable, let's call it power. Set the value to 0.
3. Multiply the current digit with (2^power), store the result.
4. Increment power by 1.
5. Set the the currentDigit to the previous digit of the hex number.
6. Repeat step 3 until all digits have been multiplied.
7. Sum the result of step 3 to get the answer number.

---------------------------------------------

Gillberk

 *SINGLE PAGE ADS*


----------



## blaqDeaph (Nov 22, 2005)

Input the binary string into an array, and use a for loop to go through each element in the array and multiply it by 2^(array element number).


----------



## deepak_99218 (Sep 21, 2008)

I assume u got the binary stored in a int variable say "val".(decimal val)

to extract each digit & also change the value use this...
temp=val%10; puts remainder in temp
val=val/10; removes the last digit from val
binary=binary+(temp*fact);
fact=fact*2; 
initially set fact=1;binary=0;
put the 4 lines in a loop until val=0;


----------

