# Binary to Decimal conversion

Page 1 of 1

## 9 Replies - 4802 Views - Last Post: 10 April 2011 - 07:00 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=226977&amp;s=8a9c1e077b52acddd3ea94a2074f6009&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jakepream

Reputation: 0
• 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()

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

## Re: Binary to Decimal conversion

Posted 10 April 2011 - 02:11 PM

Look through some of these.

### #3 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• 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.

### #4 CasiOo

• D.I.C Lover

Reputation: 1565
• Posts: 3,513
• 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

### #5 jakepream

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

### #6 jakepream

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

### #7 pbl

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

Reputation: 8376
• Posts: 31,956
• 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;
}

### #8 jakepream

Reputation: 0
• 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!!

### #9 softwareEngineer()

Reputation: -28
• 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?

### #10 pbl

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

Reputation: 8376
• Posts: 31,956
• 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;
}

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }