9 Replies - 2962 Views - Last Post: 10 April 2011 - 07:00 PM Rate Topic: -----

#1 jakepream  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-April 11

Binary to Decimal conversion

Posted 10 April 2011 - 02:03 PM

Hey guys i need help with the syntax of converting a user input decimal integer into a binary string. This is the code i have so far but can't get it to work. Also i cant use any java built in methods to do the integer to binary number conversion. So no binary to string. I'm pretty sure that my problem is in the method in the actual conversion

 import javax.swing.JOptionPane;

public class MyDecimal2Binary {
	public static void main(String[] args) {
		int value;
		
		String message, d2B;
		String myInput = JOptionPane.showInputDialog("Enter a positive integer: ");
		value = Integer.parseInt(myInput);
		d2B = decimalToBinary(value);
		 message = "Integer number " + myInput + " in Binary is " + d2B;
		 JOptionPane.showMessageDialog(null, message);


}
	public static String decimalToBinary(int value) {
		int myResult = value;
		String myString;
		while (value != 0) {
			if (value % 2 == 0)
				myResult = myResult *10;
				else myResult = (myResult * 10) + 1;
			
		
		}
						
			
			myString = Integer.toString(myResult);
			return myString;

		}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Binary to Decimal conversion

#2 softwareEngineer()  Icon User is offline

  • D.I.C Head

Reputation: -28
  • View blog
  • Posts: 247
  • Joined: 08-April 11

Re: Binary to Decimal conversion

Posted 10 April 2011 - 02:11 PM

Look through some of these.
Was This Post Helpful? 1
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Binary to Decimal conversion

Posted 10 April 2011 - 02:27 PM

Please give more details when asking for help. "It doesn't work," or "can't get it to work," are not helpful.
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1399
  • View blog
  • Posts: 3,101
  • Joined: 05-April 11

Re: Binary to Decimal conversion

Posted 10 April 2011 - 03:09 PM

Take a look at this example where 1101B is being converted:

(((((1)*2)+1)*2)+0)*2)+1 = 13


And for 1000B
((((((1)*2)+0)*2)+0)*2)+0 = 8


See the pattern?

EDIT: I'm confused.. in your topic title you write binary to decimal, and in your explaining text you write binary to decimal, but in your code you write decimal to binary :S

This post has been edited by CasiOo: 10 April 2011 - 03:27 PM

Was This Post Helpful? 0
  • +
  • -

#5 jakepream  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-April 11

Re: Binary to Decimal conversion

Posted 10 April 2011 - 04:17 PM

Yea, sorry about that i do mean decimal to binary and thanks for the link softwareEngineer() but i can't use the toBinaryString
Was This Post Helpful? 0
  • +
  • -

#6 jakepream  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-April 11

Re: Binary to Decimal conversion

Posted 10 April 2011 - 04:43 PM

I did further testing and everything but the method is working
Was This Post Helpful? 0
  • +
  • -

#7 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: Binary to Decimal conversion

Posted 10 April 2011 - 05:04 PM

The problem iss that with 0 and 1 you will quite fast reach the limit of an int value
1111111111 is the largest int you can have in binary representation

So your myResult = myResult will fail quite fast
you will have to convert it to String right away
Also you forget to divide value by / 2 after testing % 2

public static String decimalToBinary(int value) {
     if(value == 0)   // special case to have "0" for a value of 0
       return "0";

     String myString = "";
     while (value != 0) {
         if (value % 2 == 0)
             myString = "0" + myString;
         else 
             myString = "1" + myString;
         value /= 2;	
     }
     return myString;		
}


Was This Post Helpful? 1
  • +
  • -

#8 jakepream  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-April 11

Re: Binary to Decimal conversion

Posted 10 April 2011 - 05:19 PM

Thank you much for the help guys!!
Was This Post Helpful? 0
  • +
  • -

#9 softwareEngineer()  Icon User is offline

  • D.I.C Head

Reputation: -28
  • View blog
  • Posts: 247
  • Joined: 08-April 11

Re: Binary to Decimal conversion

Posted 10 April 2011 - 06:18 PM

Would you mind telling us what your solution was? It might be a good thing to have for the future.

Or was it exactly what pbl just said?
Was This Post Helpful? 0
  • +
  • -

#10 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: Binary to Decimal conversion

Posted 10 April 2011 - 07:00 PM

Actually you can get ride of the initial test for value == 0 by doing
	public static String decimalToBinary(int value) {

	     String myString = "";
	     do {
	         if (value % 2 == 0)
	             myString = "0" + myString;
	         else 
	             myString = "1" + myString;
	         value /= 2;	
	     } while (value != 0);
	     return myString;		
	}


Was This Post Helpful? 2
  • +
  • -

Page 1 of 1