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

 

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
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |