# Decimal to binary conversion

Printable View

• May 21st, 2011, 08:48 AM
baueml01
Decimal to binary conversion
I am new to the java world and I am currently taking a Programming I class. My homework assignment for the week is to write a java program using methods to allow a user to enter in a decimal number(base 10) and have the program convert it to a binary number. Just remember I am new to this so I don't know everything. I need some input on how finish finish the program I have wrote. It compiles, runs, but only allows the user to enter in a decimal number. It won't convert it. Can someone please help me with what I am doing wrong?
Here is what I have so far:

import java.util.Scanner;
public class Decimal{
public static void main (String [] args){
Scanner input = new Scanner(System.in);
System.out.println ("Enter a decimal number: ");
int decimal = input.nextInt ();
System.out.println ("Binary number is: " + decimalToBinary(decimal));
}
public static String decimalToBinary(int decimal) {
int i = 42;
String decimalToBinary = Integer.toBinaryString(i);
while (decimal !=0){
int binaryValue = decimal % 16;
decimal = decimal/16;
}
return decimalToBinary;
}
}
• May 21st, 2011, 09:37 AM
Norm
Re: Decimal to binary conversion
Quote:

It won't convert it.
What output does the program generate? Please execute the program and copy and paste here the console showing the output.
And then explain what the problem is with the output.

What is your algorithm for converting a decimal String to a binary String.
For example: "6" to "110"
• May 21st, 2011, 11:42 AM
baueml01
Re: Decimal to binary conversion
Attachment 565Attached is what the output is. It is not calculating right I don't think. When I type a number in for the input it keeps giving me a 101010 for the output. You will see in the attached file. I have searched google for the algorithm for converting a decimal to a binary and I am having no luck. I thought somebody on here who knew more would be able to help me out.
• May 21st, 2011, 11:46 AM
Norm
Re: Decimal to binary conversion
What is the purpose of this line in your program:
int i = 42;

Can you explain what each line in your decimalToBinary method is there for? Why did you code this:
int binaryValue = decimal % 16;

You need to develop an algorithm for the conversion,
If you can't find one online.
Do you understand what a binary number is?
For example what is the binary representation for 5?
When you get that, figure out how you can compute it.
Hint: use modulo 2 and divide by 2. Work from right to left in building the output.
• May 21st, 2011, 12:44 PM
baueml01
Re: Decimal to binary conversion
Quote:

Originally Posted by Norm
What is the purpose of this line in your program:
int i = 42;

Can you explain what each line in your decimalToBinary method is there for? Why did you code this:
int binaryValue = decimal % 16;

You need to develop an algorithm for the conversion,
If you can't find one online.
Do you understand what a binary number is?
For example what is the binary representation for 5?
When you get that, figure out how you can compute it.
Hint: use modulo 2 and divide by 2. Work from right to left in building the output.

Honestly I am too new at this to completely understand what exactly I am supposed to do. To be completely honest our teacher said the code we are supposed to come up with is only 3 lines different than the example she gave us out of the book. I do understand what a binary number is, I'd have to look it up because I don't remember them well enough to have it off the top of my head. I am not 100 % sure what the int i=42 is for nor the int binaryValue = decimal % 16. That is why I am looking for help. I have tried emailing my teacher and got no response. I have looked for hours online for an algorithm and help and I saw something about an intParse for converting decimal to binary but I am just so confused now that I don't know what to use. I guess I honestly don't know what to do at all. I apologize!
• May 21st, 2011, 12:56 PM
Norm
Re: Decimal to binary conversion
Even before designing an algorithm, you need to understand the problem. What is the input to the program and what is the output.
For example if the input to the program is 9 the output should be: "1001". Notice the 9 is an int value and the "1001" is a String

Before you write any code, you need to have an algorithm: the steps needed to solve the problem. First work on getting the algorithm, then figure out how to write a program to implement it.
I gave a hint at the end of my post:
For example what is the binary representation for 5?
When you get that, figure out how you can compute it.
Hint: use modulo 2 and divide by 2. Work from right to left in building the output.

Write down the binary value for 13. Then get the modulo 2 value and write it down. Then divide by 2.
Then get modulo 2 value of the new number. Then divide the new number by 2.
Continue until then new number is zero.

If you look at the modulo results they should be the binary value of the number.
• May 21st, 2011, 10:54 PM
baueml01
Re: Decimal to binary conversion
Quote:

Originally Posted by Norm
Even before designing an algorithm, you need to understand the problem. What is the input to the program and what is the output.
For example if the input to the program is 9 the output should be: "1001". Notice the 9 is an int value and the "1001" is a String

Before you write any code, you need to have an algorithm: the steps needed to solve the problem. First work on getting the algorithm, then figure out how to write a program to implement it.
I gave a hint at the end of my post:
For example what is the binary representation for 5?
When you get that, figure out how you can compute it.
Hint: use modulo 2 and divide by 2. Work from right to left in building the output.

Write down the binary value for 13. Then get the modulo 2 value and write it down. Then divide by 2.
Then get modulo 2 value of the new number. Then divide the new number by 2.
Continue until then new number is zero.

If you look at the modulo results they should be the binary value of the number.

Ok, after taking the rest of the day to figure out how to convert a decimal number into a binary number I will have to say that I can successfully do it. I am still stumped on coming up with an algorithm. I guess I just don't have enough experience with coming up with more complex algorithms. All of our other homework has been a lot more simple compared to this. We have never once talked about converting decimal to binary in class. It is something I had to figure out on my own. We talked about methods and strings for about 30-40 minutes so I am not fully aware how to use them. A lot of the terms you are using I have never even heard of (i.e. modulo 2). I have only been in class a few weeks and I have no other programming experience. So when I mean I am new, I am new to this stuff. I hate being the type of person looking for a handout but I am just not fully understanding this weeks assignment. I feel like my teacher didn't explain this section enough. My question to you is can you give me the correct code and comment in it what you are doing and why? It would also be nice to know what I am doing wrong as well. I truely need help and want to learn as this is my future and quite possibly my career so I really need to learn. I know I am asking for a lot but I have been trying to get ahold of my teacher for 3 days now and have got no response. I have also contacted 2 of my buddies (one has a masters in i.t. and works for vera bradley but has no programming experience except for sql and the other is a computer engineer for general motors and has only worked with visualbasic) he is the one who helped me with understanding the decimal to binary conversion. I truely appreciate all the help you have given me already! Thanks
• May 22nd, 2011, 06:42 AM
Norm
Re: Decimal to binary conversion
Quote:

I can successfully do it
Now write down the steps that you take to convert a decimal number to a binary one.
Make them very simple steps. Only do one thing at a time.
Given that, we can help you convert the steps to program statements.