Results 1 to 16 of 16
Thread: Binary to Decimal Converter
 11222009, 03:08 PM #1Member
 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 16bit, 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?
 11222009, 05:13 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,293
 Blog Entries
 7
 Rep Power
 24
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
 11222009, 05:20 PM #3Member
 Join Date
 Oct 2009
 Location
 Rotterdam
 Posts
 52
 Rep Power
 0
 11222009, 05:27 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,293
 Blog Entries
 7
 Rep Power
 24
 11232009, 02:43 PM #5Member
 Join Date
 Oct 2009
 Posts
 17
 Rep Power
 0
I'm sorry, I'm allowed to use System.out and System.in methods :D.
 11232009, 03:19 PM #6Member
 Join Date
 Nov 2009
 Posts
 96
 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; 11232009 at 03:42 PM.
 11232009, 03:34 PM #7Member
 Join Date
 Oct 2009
 Posts
 17
 Rep Power
 0
Can I use the substring method?
 11232009, 03:42 PM #8Member
 Join Date
 Nov 2009
 Posts
 96
 Rep Power
 0
Can I use the substring method?
 11232009, 03:47 PM #9Member
 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.
 11232009, 03:55 PM #10Member
 Join Date
 Nov 2009
 Posts
 96
 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)
 11232009, 04:07 PM #11Member
 Join Date
 Oct 2009
 Posts
 17
 Rep Power
 0
thank you very much!
 11242009, 06:42 AM #12Member
 Join Date
 Sep 2009
 Posts
 37
 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.
 11242009, 11:31 AM #13Member
 Join Date
 Oct 2009
 Posts
 17
 Rep Power
 0
Guys, any idea, how will I convert hexadecimal to decimal? Please help.
 11242009, 12:10 PM #14
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,293
 Blog Entries
 7
 Rep Power
 24
This is a complete giveaway so no explanation:
Java 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(); }
Jos
 11242009, 02:04 PM #15Member
 Join Date
 Oct 2009
 Posts
 17
 Rep Power
 0
 11242009, 03:38 PM #16
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,293
 Blog Entries
 7
 Rep Power
 24
Similar Threads

how to convert decimal value into 8bit binary value
By tOpach in forum New To JavaReplies: 4Last Post: 10262009, 11:17 PM 
Convert binary into decimal
By WarmRegards in forum New To JavaReplies: 8Last Post: 10182009, 02:32 PM 
Eclipse Decimal to binary
By queen_vee in forum New To JavaReplies: 1Last Post: 02242009, 03:17 PM 
Convert decimal to binary..pls help..newbie here
By mephisto772 in forum New To JavaReplies: 5Last Post: 02122009, 09:17 AM 
Converts a binary number to a decimal
By cachi in forum New To JavaReplies: 1Last Post: 08012007, 09:57 AM
Bookmarks