# im trying to convert binary to decimal but the math is wrong!

• (2 Pages)
• 1
• 2

## 16 Replies - 960 Views - Last Post: 05 March 2012 - 07:54 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=269572&amp;s=1fd7736321ce5946a46fbc5bc91ec14b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 iDama03

Reputation: 0
• Posts: 8
• Joined: 05-March 12

# im trying to convert binary to decimal but the math is wrong!

Posted 05 March 2012 - 05:43 PM

im having trouble doing recursion and converting binary to decimals, my binary code is on a string,
this is the code i have, i dont have any compilation or runing error, but the math is wrong, hopefully someone can help me

``` public static int  binToDec (String binary, int a)
{
//int ans = 0;
int binAns = 0;
int dec = 0;

int length = binary.length();
while(length >length-1)
{
for (int sta = length - 1; length > sta;)
{
dec = binary.charAt(sta);
binAns = binAns + (dec*(a^sta));
sta++;
return binAns;

}
return binAns = binAns + binToDec (binary, a);
}
return binAns;
}

```

This post has been edited by pbl: 05 March 2012 - 05:48 PM

Is This A Good Question/Topic? 0

## Replies To: im trying to convert binary to decimal but the math is wrong!

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: im trying to convert binary to decimal but the math is wrong!

Posted 05 March 2012 - 05:52 PM

while(length >length-1)

while(10 > 9)
that is an infinte loop as far as I can see

```           for (int sta = length - 1; length > sta;)
{
dec = binary.charAt(sta);
binAns = binAns + (dec*(a^sta));
sta++;
return binAns;   // that will return out of the method at the first iteration of the loop
}

```

so why making a for() loop ?

### #3 Sheph

• D.I.C Lover

Reputation: 447
• Posts: 1,032
• Joined: 12-October 11

## Re: im trying to convert binary to decimal but the math is wrong!

Posted 05 March 2012 - 05:53 PM

What operation do you think a^sta is doing?

### #4 iDama03

Reputation: 0
• Posts: 8
• Joined: 05-March 12

## Re: im trying to convert binary to decimal but the math is wrong!

Posted 05 March 2012 - 05:56 PM

what i try to do is convert all the strings into a int thru the for loop!
and about that infinite loop ... well on my assignment it said t has to be all the number except the last one....

i think its making 2^(power)

### #5 Sheph

• D.I.C Lover

Reputation: 447
• Posts: 1,032
• Joined: 12-October 11

## Re: im trying to convert binary to decimal but the math is wrong!

Posted 05 March 2012 - 05:58 PM

There's one problem, because ^ is the exclusive-OR operator. If you want the power function, you will have to use Math.pow(a,sta);

### #6 iDama03

Reputation: 0
• Posts: 8
• Joined: 05-March 12

## Re: im trying to convert binary to decimal but the math is wrong!

Posted 05 March 2012 - 06:04 PM

thanks but i think it didnt work, im just having a reall hard time with this since our teacher doesnt even know the subject

### #7 Sheph

• D.I.C Lover

Reputation: 447
• Posts: 1,032
• Joined: 12-October 11

## Re: im trying to convert binary to decimal but the math is wrong!

Posted 05 March 2012 - 06:07 PM

Do you have to write the code yourself? And must you use recursion? If you don't then there is no reason to make it hard for yourself. Java has built in methods to do this.
```String binaryString = "101";
int x = Integer.valueOf(binaryString, 2); // base 2 binary
String decimalString = Integer.toString(x, 10); // base 10 decimal
```

Of course, if you have to write it yourself, then that's another story. I really don't see the benefit of recursion here.

### #8 iDama03

Reputation: 0
• Posts: 8
• Joined: 05-March 12

## Re: im trying to convert binary to decimal but the math is wrong!

Posted 05 March 2012 - 06:13 PM

it is required to use recursion in our methods!
i dont even know why, but it is!

### #9 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: im trying to convert binary to decimal but the math is wrong!

Posted 05 March 2012 - 06:27 PM

The not recursive method is easy to implement

```	public static int binToInt(String bin) {
int total = 0;
for(int i = 0; i < bin.length(); ++i)
total = total * 2 + (bin.charAt(i) - '0');
}

```

### #10 iDama03

Reputation: 0
• Posts: 8
• Joined: 05-March 12

## Re: im trying to convert binary to decimal but the math is wrong!

Posted 05 March 2012 - 06:38 PM

[ public static int binToDec (String binary, int a)
{
//int ans = 0;
int binAns = 0;
int dec = 0;

int length = binary.length();
while(length >length-1)
{
for (int sta = length - 1; length > sta;)
{
dec = binary.charAt(sta);
binAns= binAns * 2 + (dec);
sta++;
return binAns;

}
return binAns = binToDec(binary, a)+ binAns;
}
return binAns;
}]
would this work too??

[
public static int binToDec (String binary, int a)
{
//int ans = 0;
int binAns = 0;
int dec = 0;

int length = binary.length();
while(length >length-1)
{
for (int sta = length - 1; length > sta;)
{
dec = binary.charAt(sta);
binAns= binAns * 2 + (dec);
sta++;
return binAns;

}
return binAns = binToDec(binary, a)+ binAns;
}
return binAns;
}
]

would this work too??

### #11 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: im trying to convert binary to decimal but the math is wrong!

Posted 05 March 2012 - 07:19 PM

and
while(length >length-1)
is still wrong

### #12 iDama03

Reputation: 0
• Posts: 8
• Joined: 05-March 12

## Re: im trying to convert binary to decimal but the math is wrong!

Posted 05 March 2012 - 07:28 PM

oh im staring to see noww!!! there is no need for that loop !!! ummm thanks it making more sense now!!

### #13 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: im trying to convert binary to decimal but the math is wrong!

Posted 05 March 2012 - 07:30 PM

My post #9 should work
You just have to convert it to a recursive method

### #14 iDama03

Reputation: 0
• Posts: 8
• Joined: 05-March 12

## Re: im trying to convert binary to decimal but the math is wrong!

Posted 05 March 2012 - 07:39 PM

thank you very much!!!!!!! i got it!!

### #15 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: im trying to convert binary to decimal but the math is wrong!

Posted 05 March 2012 - 07:41 PM