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

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 iDama03   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:48 PM

sure... its this

public static int  binToDec (String binString)
	{
		int num= 0;
		int base =2;

		int length = binString.length();
			for (int i = length - 1; length > i;)
			{
				num = Integer.parseInt(binString, base);
				if ((num== '0') && (num == '1'))
				{
			
					      num +=(Math.pow(2, base));
			     }
				i++;
				return num;
				
			}
			return num = binToDec (binStr) + num;
		
		
	}


This post has been edited by pbl: 05 March 2012 - 07:49 PM
Reason for edit:: Code tags added. One day you will learn :code:

Was This Post Helpful? 0
  • +
  • -

#17 pbl   User is offline

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

Reputation: 8381
  • View blog
  • 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:54 PM

Don't want to disappoint you but

1) you use Integer.parseInt(string, base);

you could have use only that method to do all the job, doubt your teacher gives you marks for that

2) your method is not really recursive


3) your return num;

happens at the first iteration of the for() loop so why making a loop ?

4) and the worst of it:

if ((num== '0') && (num == '1'))

How can num == '0' AND num == '1' at the same time ?

I think you still have some work to do

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

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2