Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-14-2009, 07:13 PM
WarmRegards's Avatar
Member
 
Join Date: Oct 2009
Location: Boston
Posts: 12
Rep Power: 0
WarmRegards is on a distinguished road
Send a message via AIM to WarmRegards Send a message via MSN to WarmRegards
Question Convert binary into decimal
Hey people. Ok to be honest I don't even know where to begin on this one. I have pieces of knowledge all over the place but I can't seem to put any of it together. I taught myself how to do binary to decimal conversion on paper using the "convert from binary to decimal wiki how" tutorial.

I don't need you to do my assignment for me but I need to be hinted in right directions on what I should be doing, what the next logical step would be. Preferably in plain english and code.

Here is a correct "example run" of the program:

Code:
    
    3  Input a binary number:  -1
    4  Input must be >= 0
    5
    6  Input a binary number:  1121
    7  Input must be a Binary number
    8
    9  Input a binary number:  0011100
   10  11100
   11  Base 2 Equals
   12  28
   13  Base 10
Here is what I have so far:

Code:
import java.util.*; //Scanner class is defined here

public class BinaryToDecimal {

	public static void main(String[] args) {
		BinaryToDecimal obj = new BinaryToDecimal(); 
		obj.process(); 
	}

	private void process(){
		Scanner in = new Scanner(System.in); //required to read input
		System.out.print("Input a binary number: ");
		int number = in.nextInt();	     //reads a line of input
		
		while (number < 0){  
			System.out.println("Input must be >= 0");
			System.out.print("\nInput a binary number: ");
			number = in.nextInt(); //accepts new keyboard input again
					
		}
	}
}
Other rules:

* You may not use any of the Math class built in power() or binary digit processing methods. All calculations must be done explicitly.

* We do not need to use arrays.

It's best to use:

* % (modulus operator) to access the current right-most digit

* / (int division operator) to divide down the current value after processing the right-most digit

* Repeat for all digits

This is the best and easiest way to solve this assignment.
__________________
"All animals are equal, but some are more equal than others."
- George Orwell
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 10-14-2009, 07:18 PM
xcallmejudasx's Avatar
Senior Member
 
Join Date: Oct 2008
Location: Houston, TX & Flint, MI
Posts: 585
Rep Power: 2
xcallmejudasx is on a distinguished road
Send a message via AIM to xcallmejudasx
Default
If I remember correctly you just keep modding the number by 2 and if you get a remainder you put a 1 and if you dont you put a 0. I think at the end you have to reverse the entire string for it to output correctly.
__________________
Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-14-2009, 07:24 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,465
Rep Power: 8
Fubarable is on a distinguished road
Default
Heck, I'd use the static parseInteger method from the Integer class to parse the input String using a radix of 2. It doesn't say you can't do it that way, but I'm guessing the instructor won't like the solution.
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-14-2009, 07:24 PM
WarmRegards's Avatar
Member
 
Join Date: Oct 2009
Location: Boston
Posts: 12
Rep Power: 0
WarmRegards is on a distinguished road
Send a message via AIM to WarmRegards Send a message via MSN to WarmRegards
Default
I should note I have a total lifetime experience of 4 weeks with programming in Java and programming at all.
__________________
"All animals are equal, but some are more equal than others."
- George Orwell
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 10-14-2009, 07:27 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,465
Rep Power: 8
Fubarable is on a distinguished road
Default
Originally Posted by xcallmejudasx View Post
If I remember correctly you just keep modding the number by 2 and if you get a remainder you put a 1 and if you dont you put a 0. I think at the end you have to reverse the entire string for it to output correctly.
Doesn't that solve the inverse problem? Or is my brain not working today?

Original Poster: what steps would you do to solve this on paper?

If I couldn't use Integer.parseInt, I'd do what the professor states:

1) get the right most number by mod 2,
2) then divide the result by 2 and do something with the result.
3) Get the next right most number by dividing the original number by 2, then modding by 2,...
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags

Last edited by Fubarable; 10-14-2009 at 07:31 PM.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 10-14-2009, 07:34 PM
xcallmejudasx's Avatar
Senior Member
 
Join Date: Oct 2008
Location: Houston, TX & Flint, MI
Posts: 585
Rep Power: 2
xcallmejudasx is on a distinguished road
Send a message via AIM to xcallmejudasx
Default
Originally Posted by Fubarable View Post
Doesn't that solve the inverse problem? Or is my brain not working today?

Original Poster: what steps would you do to solve this on paper?
The inverse being converting from ....I just caught myself.

Ya ignore my previous post it doesn't answer the question you asked.

There's a simple method I learned(once again it's been a while this could be wrong) but to convert from binary to decimal you take the 2's place value of the digit, 11010 being 32 16 8 4 2 and if you have a 1 at that location you add the number. so 11010 would be 2^5 + 2^4 + 0 + 2^2 + 2 = 32 + 16 + 0 + 4 + 0 = 52
__________________
Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 10-14-2009, 07:34 PM
WarmRegards's Avatar
Member
 
Join Date: Oct 2009
Location: Boston
Posts: 12
Rep Power: 0
WarmRegards is on a distinguished road
Send a message via AIM to WarmRegards Send a message via MSN to WarmRegards
Default
well on paper what I would do, for instance is take a random digit like:

10111001

Then starting on the right side, double as you work your way to the left.

So:

1 = 1
0 = 2
0 = 4
1 = 8
1 = 16
1 = 32
0 = 64
1 = 128

Then, what we really care about are the values associated with 1's, so 128, 32, 16, 8 and 1.

Add those up: 128+32+16+8+1 = 185
__________________
"All animals are equal, but some are more equal than others."
- George Orwell
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 10-14-2009, 07:39 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,465
Rep Power: 8
Fubarable is on a distinguished road
Default
OK, now you need to try to find a way to isolate the ones in the 0/2/4/8/16/... placeholders. And I was wrong, you'll need to treat the number as a non-binary one to start with using divide by 10, 100, 1000 etc. This is your program and you should play with this to see what happens when you do this. Also, I would use a boolean for my while loop, and would only ask for input from within the while loop. something like so:

Code:
    boolean inputOK = false;
    while (!inputOK) {
      System.out.print("Please input a binary number: ");
      //.....
    }
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 10-18-2009, 03:32 PM
Member
 
Join Date: Sep 2009
Posts: 37
Rep Power: 0
raqman is on a distinguished road
Default
Tty the codes below:
import java.lang.*;
import java.io.*;

public class BinaryToDecimal{
public static void main(String[] args) throws IOException{
BufferedReader bf= new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the Binary value: ");
String str = bf.readLine();
long num = Long.parseLong(str);
long rem;
while(num > 0){
rem = num % 10;
num = num / 10;
if(rem != 0 && rem != 1){
System.out.println("This is not a binary number.");
System.out.println("Please try once again.");
System.exit(0);
}
}
int i= Integer.parseInt(str,2);
System.out.println("Decimal:="+ i);
}
}
__________________
RAQ Report: free Java reporting tool.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] how to convert decimal value into 8-bit binary value tOpach New To Java 4 10-26-2009 11:17 PM
converting decimal to binary value using recursion in java Anindo New To Java 3 07-25-2009 02:44 PM
Eclipse- Decimal to binary queen_vee New To Java 1 02-24-2009 03:17 PM
Convert decimal to binary..pls help..newbie here mephisto772 New To Java 5 02-12-2009 09:17 AM
Converts a binary number to a decimal cachi New To Java 1 08-01-2007 10:57 AM


All times are GMT +2. The time now is 06:13 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org