C | Help Converting Binary to Decimal

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

r3drock3t88

Thread Starter
Joined
Jan 12, 2007
Messages
272
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...
 
Joined
Sep 12, 2003
Messages
20,583
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...
Hi r3drock3t88,

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

-- Tom
 
Joined
Aug 12, 2007
Messages
305
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.
Code:
{
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

Banned
Joined
Dec 9, 2004
Messages
24,388
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.
 
Joined
Aug 12, 2007
Messages
305
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.
 
Joined
Sep 12, 2003
Messages
20,583
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
 
Joined
Jul 28, 2008
Messages
1,899
I get what you want :D 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 :D
 
Joined
Sep 18, 2008
Messages
2
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
 
Joined
Nov 22, 2005
Messages
869
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).
 
Joined
Sep 21, 2008
Messages
6
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;
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Top