
11-22-2009, 03:08 PM
|
|
Member
|
|
Join Date: Oct 2009
Posts: 17
Rep Power: 0
|
|
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?
|
|

11-22-2009, 05:13 PM
|
|
Senior Member
|
|
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 930
Rep Power: 2
|
|
|
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
|
|

11-22-2009, 05:20 PM
|
|
Member
|
|
Join Date: Oct 2009
Location: Rotterdam
Posts: 39
Rep Power: 0
|
|
Originally Posted by JosAH
|
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.
|
|

11-22-2009, 05:27 PM
|
|
Senior Member
|
|
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 930
Rep Power: 2
|
|
Originally Posted by Arnold
|
|
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
|
|

11-23-2009, 02:43 PM
|
|
Member
|
|
Join Date: Oct 2009
Posts: 17
Rep Power: 0
|
|
I'm sorry, I'm allowed to use System.out and System.in methods  .
|
|

11-23-2009, 03:19 PM
|
|
Member
|
|
Join Date: Nov 2009
Posts: 84
Rep Power: 0
|
|
|
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.
|
|

11-23-2009, 03:34 PM
|
|
Member
|
|
Join Date: Oct 2009
Posts: 17
Rep Power: 0
|
|
|
Can I use the substring method?
|
|

11-23-2009, 03:42 PM
|
|
Member
|
|
Join Date: Nov 2009
Posts: 84
Rep Power: 0
|
|
|
Quote:
|
|
Can I use the substring method?
|
Yes you can, but to do what?
|
|

11-23-2009, 03:47 PM
|
|
Member
|
|
Join Date: Oct 2009
Posts: 17
Rep Power: 0
|
|
|
to isolate the 0b or 0x and specify the other part of the string that would convert.
|
|

11-23-2009, 03:55 PM
|
|
Member
|
|
Join Date: Nov 2009
Posts: 84
Rep Power: 0
|
|
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)
|
|

11-23-2009, 04:07 PM
|
|
Member
|
|
Join Date: Oct 2009
Posts: 17
Rep Power: 0
|
|
|
thank you very much!
|
|

11-24-2009, 06:42 AM
|
|
Member
|
|
Join Date: Sep 2009
Posts: 38
Rep Power: 0
|
|
|
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.
|
|

11-24-2009, 11:31 AM
|
|
Member
|
|
Join Date: Oct 2009
Posts: 17
Rep Power: 0
|
|
|
Guys, any idea, how will I convert hexadecimal to decimal? Please help.
|
|

11-24-2009, 12:10 PM
|
|
Senior Member
|
|
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 930
Rep Power: 2
|
|
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
|
|

11-24-2009, 02:04 PM
|
|
Member
|
|
Join Date: Oct 2009
Posts: 17
Rep Power: 0
|
|
Thank you very much for your help but I'm still confused with the codes. Can you please enlighten me  .
Originally Posted by JosAH
|
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
|
|
|

11-24-2009, 03:38 PM
|
|
Senior Member
|
|
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 930
Rep Power: 2
|
|
Originally Posted by c_walker
|
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
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 03:40 PM.
|
|