# Decimal to Binary Conversion Program

Page 1 of 1

## 7 Replies - 11348 Views - Last Post: 26 March 2012 - 06:13 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=272598&amp;s=6927ffc88a99ab9d56b898018ab4235b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 BigD959919

Reputation: 0
• Posts: 91
• Joined: 26-March 12

# Decimal to Binary Conversion Program

Posted 26 March 2012 - 02:40 PM

I apologize if this is in the wrong place but I am new here so please be gentle. This IS for a project but I have done all of the work I will be posting here in a moment. My code so far is as follows:

```// ###########################
// Project 4: DecimalConversion.java
// Grounds for extra credit:

import java.util.*;
import java.lang.*;

public class DecimalConversion {
public static void main(String[] args) {
long decimal, binary = 0;
int i = 0;

for (i = 0; i < 1000000; i++) {
System.out.print("Enter a decimal integer: ");
Scanner input = new Scanner(System.in);
decimal = input.nextLong();
if (decimal == -1) {
System.out.println("All set!");
System.exit(0);
} else
binary = Binary(decimal);
System.out.println("Conversion to binary: " + binary);
}
}
public static long Binary(long decimal) {
long binary = 0;

while (decimal > 0) {
binary = binary*10+decimal%2;
decimal = decimal/2;
}
return binary;
}
}
```

The program compiles and runs however I am losing zeros, for example here is a copy/past of the debugger:
Enter a decimal integer: 14
Conversion to binary: 111

I tried google searching for an answer but all I seem to find is a program that uses the java conversion functions which I am NOT allowed to use for this particular program. Any help would be much appreciated.

Is This A Good Question/Topic? 0

## Replies To: Decimal to Binary Conversion Program

### #2 codeMonkey_1066

Reputation: 19
• Posts: 71
• Joined: 22-March 12

## Re: Decimal to Binary Conversion Program

Posted 26 March 2012 - 03:36 PM

The algorithm your using is producing the binary digits in the revers order.
so 14 which is 1110 in binary, your code is producing 0111, though you don't see the leading zero.
Try the code with 13 which is 1101 in binary , your code produces 1011

You may want to look at Bitwise and Bit Shift Operators ( more general wikipedia
artical
)

Also I think you'll be better off building up a string of 1s and 0s rather than a long.

If you happen you be using java 7 you also have access to binary literals which would be useful for testing.
``` System.out(toBinary(13)); // should output 1101
System.out(toBinary(0b1101); // exactly as above but easier to see what the output should be.

```

### #3 BigD959919

Reputation: 0
• Posts: 91
• Joined: 26-March 12

## Re: Decimal to Binary Conversion Program

Posted 26 March 2012 - 03:48 PM

okay so my code is reversing the binary code...got it. Is it a calculation error or is there a way to flip the sequence?

### #4 codeMonkey_1066

Reputation: 19
• Posts: 71
• Joined: 22-March 12

## Re: Decimal to Binary Conversion Program

Posted 26 March 2012 - 03:59 PM

You're producing the right digits, you just have to display them in the correct order.

Try building up the result as a String rather than as a number.

### #5 pbl

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

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

## Re: Decimal to Binary Conversion Program

Posted 26 March 2012 - 05:22 PM

And no need to create 100000 Scanner objects

Scanner input = new Scanner(System.in);

create only one BEFORE the for() loop

### #6 BigD959919

Reputation: 0
• Posts: 91
• Joined: 26-March 12

## Re: Decimal to Binary Conversion Program

Posted 26 March 2012 - 05:58 PM

Quote

// Dylan Peters
// Project 4: DecimalConversion.java
// Grounds for extra credit:

import java.util.*;

public class DecimalConversion {
public static void main(String[] args) {
int decimal;
String binary = "0";

Scanner input = new Scanner(System.in);
for (int i = 0; i < 1000000; i++) {
System.out.print("Enter a decimal integer: ");
decimal = input.nextInt();
if (decimal == -1) {
System.out.println("All set!");
return;
} else
binary = Binary(decimal);
System.out.println("Conversion to binary: " + binary);
}
}
public static String Binary(int decimal) {
String binary_string = "0";
int binary = 0;

while (decimal > 0) {
binary = binary*10+decimal%2;
decimal = decimal/2;
binary_string = Integer.toString(binary);
}
return binary_string;
}
}

So this is where I am at now. I still am having issues with the zeros. I think I resolved the string issue but as I am really bad at this I can't say for certain.

Lets try this again...

```// Dylan Peters
// Project 4: DecimalConversion.java
// Grounds for extra credit:

import java.util.*;

public class DecimalConversion {
public static void main(String[] args) {
int decimal;
String binary = "0";

Scanner input = new Scanner(System.in);
for (int i = 0; i < 1000000; i++) {
System.out.print("Enter a decimal integer: ");
decimal = input.nextInt();
if (decimal == -1) {
System.out.println("All set!");
return;
} else
binary = Binary(decimal);
System.out.println("Conversion to binary: " + binary);
}
}
public static String Binary(int decimal) {
String binary_string = "0";
int binary = 0;

while (decimal > 0) {
binary = binary*10+decimal%2;
decimal = decimal/2;
binary_string = Integer.toString(binary);
}
return binary_string;
}
}
```

### #7 pbl

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

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

## Re: Decimal to Binary Conversion Program

Posted 26 March 2012 - 06:08 PM

Complicated for nothing

```	public static String Binary(int decimal) {
String binary_string = "";

while (decimal > 0) {
binary_string = decimal%2 + binary_string;
decimal /= 2;
}
return binary_string;
}
}

```

### #8 BigD959919

Reputation: 0
• Posts: 91
• Joined: 26-March 12

## Re: Decimal to Binary Conversion Program

Posted 26 March 2012 - 06:13 PM

I see my error. Thank you for the help.