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

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 749 Views - Last Post: 05 March 2012 - 07:54 PM Rate Topic: -----

#1 iDama03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 :D

 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
Reason for edit:: Code tags added. Please :code:


Is This A Good Question/Topic? 0
  • +

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

#2 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,857
  • 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 ?
Was This Post Helpful? 0
  • +
  • -

#3 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • 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?
Was This Post Helpful? 1
  • +
  • -

#4 iDama03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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)
Was This Post Helpful? 0
  • +
  • -

#5 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • 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);
Was This Post Helpful? 1
  • +
  • -

#6 iDama03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#7 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • 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.
Was This Post Helpful? 0
  • +
  • -

#8 iDama03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,857
  • 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');
                return total;
	}


Was This Post Helpful? 0
  • +
  • -

#10 iDama03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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??
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

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

Posted 05 March 2012 - 07:19 PM

Please: :code:

and
while(length >length-1)
is still wrong
Was This Post Helpful? 1
  • +
  • -

#12 iDama03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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!! :D
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,857
  • 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
Was This Post Helpful? 0
  • +
  • -

#14 iDama03  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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!! :clap:
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

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

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

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

Posted 05 March 2012 - 07:41 PM

Glad I could help
Love to see your recursive solution :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2