programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Lost code Binary to decimal using array

katrina arquiza
Greenhorn
Posts: 3
Hello,

im stuck at my code for our lab Homework. Please help me. I do not know how to convert a reversed array to decimal.

The output should be:

How many digits to convert?
(user will input) ex. 3
(user will input 3 digits)
ex. 0
1
1
The binary digits are: 110 (<-reversed)
The decimal value is: 6

Here is my code:

subodh kureel
Greenhorn
Posts: 27

where mbinary is binary digit variable...otherwise its basic calculation to convert binary to decimal.

while(true){
if(mbinary == 0){
break;
} else {
int temp = mbinary%10;
decimal += temp*Math.pow(2, power);
mbinary = mbinary/10;
power++;
}
}

katrina arquiza
Greenhorn
Posts: 3
those variables are not declared and where will i add it? thank you

katrina arquiza
Greenhorn
Posts: 3
Output:

Welcome to Number Systems Conversion
a. Binary to Decimal
b. Octal to Decimal
How many digits to convert?
3
0
1
1
The binary number is: 110
Decimal value is: 3

Campbell Ritchie
Marshal
Posts: 56599
172
• 2
subodh kureel wrote:try to add this code..
. . .
I would recommend you try to avoid such code. For a start you should get suspicious of anything with while (true) in.

subodh kureel
Greenhorn
Posts: 27
Campbell , I do agree ..while(true) should be avoided..my bad..thanks for pointing it put..

Katrina,
Please try below..i can fix your code but i believe objective is to provide the guidance.. hope this help...

Campbell Ritchie
Marshal
Posts: 56599
172
Yes, the objective is to provide guidance, but you are not doing that. You are providing a solution. That does not help the OP learn at all. I suspect the damage has been done and it is too late to undo, but by providing a complete solution you have “excused” OP from thinking through her submission. Look what it says on the contents page of this forum:-
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
Don't be annoyed with me, but I have pulled rank and deleted your solution.

I might replace it in a couple of days, and ask you to explain it, because I am not convinced it is a particularly good solution.

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.