Results 1 to 16 of 16
  1. #1
    c_walker is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    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?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    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

  3. #3
    Arnold is offline Member
    Join Date
    Oct 2009
    Location
    Rotterdam
    Posts
    52
    Rep Power
    0

    Exclamation

    Quote 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:
    Java Code:
    int i = Interger.parseInt("0110011011100110", 2);
    What he needs is a static field with powers of 2, and the charAt() method.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote 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

  5. #5
    c_walker is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    I'm sorry, I'm allowed to use System.out and System.in methods :D.

  6. #6
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    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.

  7. #7
    c_walker is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    Can I use the substring method?

  8. #8
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    Can I use the substring method?
    Yes you can, but to do what?

  9. #9
    c_walker is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    to isolate the 0b or 0x and specify the other part of the string that would convert.

  10. #10
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    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)

  11. #11
    c_walker is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    thank you very much!

  12. #12
    raqman is offline Member
    Join Date
    Sep 2009
    Posts
    37
    Rep Power
    0

    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.

  13. #13
    c_walker is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    Guys, any idea, how will I convert hexadecimal to decimal? Please help.

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    This is a complete give-away 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();
    }
    kind regards,

    Jos

  15. #15
    c_walker is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    Thank you very much for your help but I'm still confused with the codes. Can you please enlighten me :confused:.

    Quote Originally Posted by JosAH View Post
    This is a complete give-away 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();
    }
    kind regards,

    Jos

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote 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 :confused:.
    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

Similar Threads

  1. Replies: 4
    Last Post: 10-26-2009, 11:17 PM
  2. Convert binary into decimal
    By WarmRegards in forum New To Java
    Replies: 8
    Last Post: 10-18-2009, 03:32 PM
  3. Eclipse- Decimal to binary
    By queen_vee in forum New To Java
    Replies: 1
    Last Post: 02-24-2009, 03:17 PM
  4. Convert decimal to binary..pls help..newbie here
    By mephisto772 in forum New To Java
    Replies: 5
    Last Post: 02-12-2009, 09:17 AM
  5. Converts a binary number to a decimal
    By cachi in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 10:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •