• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Convert binary to decimal

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


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

 
getting peopel to do your homwwork for you isnt exactly highly technical, especially when it only requires a loop with 1 line of code...
 
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?
 
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 😛
 
Back
Top