Convert binary to decimal

Saint Nick

Lifer
Jan 21, 2005
17,722
6
81
I can figure out how to convert decimal to binary using mathmatical calculations, but for some reason I can't figure out how to work backwards.

123 is 1111011 in binary.

1 means it has a remainder, 0 means it doesn't (I'm sure you would have figured this out.)

123 / 2 = 1
61 / 2 = 1
30 / 2 = 0
15 / 2 = 1
7 / 2 = 1
3 / 2 = 1
1 / 2 = 1

So how do I work this backwards?
 

SamurAchzar

Platinum Member
Feb 15, 2006
2,422
3
76
Iterate over all binary digits, for each '1' add 2^N to the sum, where N is the place of the digit (from right to left - LSB to MSB), counting from 0.

So in your case that would be
1: 2^0 +
1: 2^1 +
0:
1: 2^3 +
1: 2^4 +
1: 2^5 +
1: 2^6
-------
123


 

Marthisdil

Senior member
Aug 13, 2001
443
0
71
I always remembered to just label the columns


128 64 32 16 8 4 2 1

Filling in from right to left (because it's binary)

1 1 1 1 0 1 1

Every place there's a 1, you add it's base to - 64 + 32 + 16 + 8 + 2 + 1 = 123

M
 

Marthisdil

Senior member
Aug 13, 2001
443
0
71
I always remembered to just label the columns


Code:
128   64   32   16   8   4   2   1

Filling in from right to left (because it's binary)

         1     1     1    1   0   1   1
Every place there's a 1, you add it's base to - 64 + 32 + 16 + 8 + 2 + 1 = 123

M
 

SamurAchzar

Platinum Member
Feb 15, 2006
2,422
3
76
That's what I always do too - but it's not educational to tell people that straight up :D

 

Saint Nick

Lifer
Jan 21, 2005
17,722
6
81
Well, long story short, I am supposed to write a C++ program that converts up to a fifteen digit binary number into a decimal number. It doesn't have to validate the information or anything, just assuming that the user enters 1s and 0s and only up to 15 of them total. I can't figure out how to do it. This helped, but everything I've tried thus far (code-wise of course) has done me no good.

bleh . . . why am I a comp sci major?
 

jakalsucks

Member
Oct 29, 2005
31
0
0
Forgive me, I haven't written in C++ in a very long time. I understand the syntax isn't anywhere near right.

for(intCounterX = 0; intCounterX < strTemp.Length; intCounterX++)
{
intTotal = (intTotal << 1) + (int)strTemp[intCounterX] - 48
}

That last post I made was complete rubish.

 

BrownTown

Diamond Member
Dec 1, 2005
5,314
1
0
getting peopel to do your homwwork for you isnt exactly highly technical, especially when it only requires a loop with 1 line of code...
 

Vegitto

Diamond Member
May 3, 2005
5,234
1
0
Originally posted by: jndietz
Well, long story short, I am supposed to write a C++ program that converts up to a fifteen digit binary number into a decimal number. It doesn't have to validate the information or anything, just assuming that the user enters 1s and 0s and only up to 15 of them total. I can't figure out how to do it. This helped, but everything I've tried thus far (code-wise of course) has done me no good.

bleh . . . why am I a comp sci major?

I don't know any C(+/++), but why don't you just use a language in between? Like, when you want to BabelFish something from French to English, the engine translates the French to Esperanto, and the Esperanto to English. Maybe binary > hex > decimal?
 

BassBomb

Diamond Member
Nov 25, 2005
8,390
1
81
i have lost all my knowledge from grade 11 when i learned this

but id use my Sharp DAL calc to convert if i needed to :p