Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-22-2009, 03:08 PM
Member
 
Join Date: Oct 2009
Posts: 17
Rep Power: 0
c_walker is on a distinguished road
Unhappy Binary to Decimal Converter
Hi everyone. I need help with my Java program. I was tasked to create a program that converts an inputed 16-bit, 2's complement binary or hexadecimal number into decimal number. Included in the input is the 0b and 0x to indicate whether its binary or hexadecimal. The main rule for the program is that I only have to use the String and Scanner classes. Also, I was restricted to use only the hasNext() and nextLine methods for the Scanner class. Do you have any tips on how to make the program?
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-22-2009, 05:13 PM
Senior Member
 
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 1,247
Rep Power: 3
JosAH is on a distinguished road
Default
Have a look at the Short or Integer classes; both can parse a String as if it were an integer number in (almost) any radix and they can produce a String representing a short (or int) in (almost) any radix; you have to get rid of the prefix 0b or 0x before the parsing but those prefixes tell you wich radix to use.

kind regards,

Jos
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-22-2009, 05:20 PM
Member
 
Join Date: Oct 2009
Location: Rotterdam
Posts: 39
Rep Power: 0
Arnold is on a distinguished road
Exclamation
Originally Posted by JosAH View Post
Have a look at the Short or Integer classes; both can parse a String as if it were an integer number in (almost) any radix and they can produce a String representing a short (or int) in (almost) any radix; you have to get rid of the prefix 0b or 0x before the parsing but those prefixes tell you wich radix to use.

kind regards,

Jos
I believe he said he could not use the Integer class. Otherwise he could do something like this:
Code:
int i = Interger.parseInt("0110011011100110", 2);
What he needs is a static field with powers of 2, and the charAt() method.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-22-2009, 05:27 PM
Senior Member
 
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 1,247
Rep Power: 3
JosAH is on a distinguished road
Default
Originally Posted by Arnold View Post
I believe he said he could not use the Integer class.
Yep, only two classes are allowed and the System class is not one of them so there's no output from that program ;-) Oh well, it isn't that hard to parse/generate a String given a certain radix.

kind regards,

Jos
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-23-2009, 02:43 PM
Member
 
Join Date: Oct 2009
Posts: 17
Rep Power: 0
c_walker is on a distinguished road
Default
I'm sorry, I'm allowed to use System.out and System.in methods .
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-23-2009, 03:19 PM
sky sky is offline
Member
 
Join Date: Nov 2009
Posts: 95
Rep Power: 0
sky is on a distinguished road
Default
Maybe you are asked to program the "manual" algorithm. If you have as input an String and you cannot use parseInt, one method is to read the String char by char and to substract the ASCII code. Useful ASCII codes are 0x30 for 0 and 0x65 for A if I don't remember bad (just look for a table in Google).

After that, in order to convert from binary to decimal just get rid of 0b and then start dividing by two and keeping the reminders of the division. If you transverse the order you get the decimal number.

Last edited by sky; 11-23-2009 at 03:42 PM.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-23-2009, 03:34 PM
Member
 
Join Date: Oct 2009
Posts: 17
Rep Power: 0
c_walker is on a distinguished road
Default
Can I use the substring method?
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 11-23-2009, 03:42 PM
sky sky is offline
Member
 
Join Date: Nov 2009
Posts: 95
Rep Power: 0
sky is on a distinguished road
Default
Quote:
Can I use the substring method?
Yes you can, but to do what?
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 11-23-2009, 03:47 PM
Member
 
Join Date: Oct 2009
Posts: 17
Rep Power: 0
c_walker is on a distinguished road
Default
to isolate the 0b or 0x and specify the other part of the string that would convert.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 11-23-2009, 03:55 PM
sky sky is offline
Member
 
Join Date: Nov 2009
Posts: 95
Rep Power: 0
sky is on a distinguished road
Default
Yes, thats one possibility. I would recommend you to use first the trim() method to remove spaces from the beginning and end. Then, you can use the startsWith() method to make sure your String begins with "0b" and finally the substring() method starting from index 1 to return the other part of the string. Another possibilities are the methods replace() or replaceAll(). Have a look at this website to know the descriptions of the String methods:

String (Java 2 Platform SE v1.4.2)
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 11-23-2009, 04:07 PM
Member
 
Join Date: Oct 2009
Posts: 17
Rep Power: 0
c_walker is on a distinguished road
Default
thank you very much!
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 11-24-2009, 06:42 AM
Member
 
Join Date: Sep 2009
Posts: 37
Rep Power: 0
raqman is on a distinguished road
Default
EXAMPLE:

(11010.01) to Decimal -

take each number invdividually and multiply it by 2 to the power of the placement of the number - 1. (5 places to the left of the decimal would be 1 * 2/\4)

so you start with the number far left and move right like this :

(1 * 2/\4) + (1 * 2/\3) + (0 * 2/\2) + (1 * 2/\1) + (0 * 2/\0) + (0 * 2/\-1) + (1 * 2/\-2) = 26.25

so thats how you would go from binary to decimal...so you will need to write a program that will use that basic formula.

hope that makes some sense...
__________________
RAQ Report: free Java reporting tool.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 11-24-2009, 11:31 AM
Member
 
Join Date: Oct 2009
Posts: 17
Rep Power: 0
c_walker is on a distinguished road
Default
Guys, any idea, how will I convert hexadecimal to decimal? Please help.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 11-24-2009, 12:10 PM
Senior Member
 
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 1,247
Rep Power: 3
JosAH is on a distinguished road
Default
This is a complete give-away so no explanation:

Code:
private static final String digits= "0123456789abcdef";
	
static int toNumber(String s, int radix) {
	
	int num= 0;
	for (int i= 0, n= s.length(); i < n; i++)
		num= num*radix+digits.indexOf(s.charAt(i));
		
	return num;
}
	
static String toString(int num, int radix) {
		
	StringBuilder sb= new StringBuilder();
		
	for(; num != 0; num/= radix)
		sb.insert(0, digits.charAt(num%radix));
		
	return sb.length() == 0?"0":sb.toString();
}
kind regards,

Jos
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 11-24-2009, 02:04 PM
Member
 
Join Date: Oct 2009
Posts: 17
Rep Power: 0
c_walker is on a distinguished road
Default
Thank you very much for your help but I'm still confused with the codes. Can you please enlighten me .

Originally Posted by JosAH View Post
This is a complete give-away so no explanation:

Code:
private static final String digits= "0123456789abcdef";
	
static int toNumber(String s, int radix) {
	
	int num= 0;
	for (int i= 0, n= s.length(); i < n; i++)
		num= num*radix+digits.indexOf(s.charAt(i));
		
	return num;
}
	
static String toString(int num, int radix) {
		
	StringBuilder sb= new StringBuilder();
		
	for(; num != 0; num/= radix)
		sb.insert(0, digits.charAt(num%radix));
		
	return sb.length() == 0?"0":sb.toString();
}
kind regards,

Jos
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 11-24-2009, 03:38 PM
Senior Member
 
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 1,247
Rep Power: 3
JosAH is on a distinguished road
Default
Originally Posted by c_walker View Post
Thank you very much for your help but I'm still confused with the codes. Can you please enlighten me .
I already have been spoonfeeding you; it's your turn now: read the detailed description of the used methods and classes in the API documentation; that code isn't rocket science.

kind regards,

Jos
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
Convert binary into decimal WarmRegards New To Java 8 10-18-2009 03:32 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 03:18 PM.



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