Results 1 to 15 of 15
  1. #1
    gamer765 is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Default Need help modifying code for hex conversion

    Hi everyone. I'm new to Java and look forward to learning alot from here.
    Currently I have a little problem with my code. I need to modify my existing java code which converts a base 10 value to base 10 and below. I need help modifying my code to allow for hex conversion also. My instructions are: Modify your program to handle base conversions up to base 16 (hexadecimal). Use if statements to substitute letters for numbers when the remainders are 10 or greater (for example, a remainder of 10 should be represented as A).
    My code is the following:

    Java Code:
    import java.util.*; // needed for Scanner
    
    public class Converter
    {
    	public static void main (String [ ] args)
    	{
    		// Use this Scanner to read from the keyboard
    		Scanner input = new Scanner(System.in);
    		String answer = "";
    		System.out.print("Enter base 10 value to convert: ");
    		int value = input.nextInt();
    		System.out.print("Enter base to translate into: ");
    		int n = input.nextInt();
    		int quotient;
    		int remainder;
    		
    		while (value>0)
    		{
    			quotient = value / n;
    			remainder = value % n;
    			value = quotient;
    			answer = remainder + answer;
    		}
    		System.out.println("Answer: " +answer);
    	}
    }
    I do not need a complete rewrite, but an addon to the code to allow the conversion.
    Thank you for your help.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    try:

    int remainder; --> char remainder;
    remainder = value % n; --> remainder = (char) ((value % n) > 9 ? ((value % n) % 10) + 65 : (value % n) + 48);

    D:D

  3. #3
    gamer765 is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Default

    Thanks for the help, but the instructions say to use if statements.

  4. #4
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    Inside the while loop you can just create 5 separate if statements:

    Java Code:
    if(remainder = 10)
    if(remainder = 11)
    etc...

    Then you just need a character to hold the hex value like eRaaaa said.
    Sincerely, Joshua Green
    Please REP if I help :)

  5. #5
    gamer765 is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Default

    care to give an example as to how I would write this?

  6. #6
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    Here's a little help to get you started, and actually I guess you won't need a char variable:

    Java Code:
    if(remainder == 10)
         answer += 'A';
    Sincerely, Joshua Green
    Please REP if I help :)

  7. #7
    gamer765 is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Default

    Thanks. I'll see how I write this.

  8. #8
    gamer765 is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Default

    I can now get the letters to print normally, but they are in the wrong place. They dont seem to replace the remainder value and are added at the end instead (eg. converting 1000 to base 16 yields 3148E)

  9. #9
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    What is the code you are currently using?

  10. #10
    gamer765 is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Default

    I am using this as my code so far

    Java Code:
    import java.util.*; // needed for Scanner
    
    public class Converter
    {
    	public static void main (String [ ] args)
    	{
    		// Use this Scanner to read from the keyboard
    		Scanner input = new Scanner(System.in);
    		String answer = "";
    		System.out.print("Enter base 10 value to convert: ");
    		int value = input.nextInt();
    		System.out.print("Enter base to translate into: ");
    		int n = input.nextInt();
    		int quotient;
    		int remainder;
    		
    		while (value>0)
    		{
    			quotient = value / n;
    			remainder = value % n;
    			value = quotient;
    			if (remainder == 10)
    				answer += 'A';
    			if (remainder == 11)
    				answer += 'B';
    			if (remainder == 12)
    				answer += 'C';
    			if (remainder == 13)
    				answer += 'D';
    			if (remainder == 14)
    				answer += 'E';
    			if (remainder == 15)
    				answer += 'F';
    			answer = remainder + answer;
    		}
    		System.out.println("Answer: " +answer);
    	}
    }

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Hm, what if you swap your String concatenation:

    Java Code:
    answer = "F" + answer;
    but really... you should be able to figure things like this out, right?

  12. #12
    gamer765 is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Default

    well not really. We haven't gone over this in lecture yet.

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by gamer765 View Post
    well not really. We haven't gone over this in lecture yet.
    It has nothing to do with your lecture and all to do with basic logic. If you're adding to the right of a String and dissatisfied that stuff is getting added to the right, simple logic states that you should add to the other side of the String. It basically means you should experiment with your code and try these things out.
    Last edited by Fubarable; 10-24-2010 at 10:28 PM.

  14. #14
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Quote Originally Posted by gamer765 View Post
    Thanks for the help, but the instructions say to use if statements.
    only a small note:
    the ternary operator is a shorthand for an if-then-else statement. (i think you have only two cases here, why should you check more than these two? (except as an exercise)
    its the same if you would write
    Java Code:
    				int mod = value % n;
    				if (mod > 9) {
    					remainder = (char) (mod % 10 + 65);
    				} else {
    					remainder = (char) (mod + 48);
    				}
    (remember, change remainder to char *g*)
    A-F are ASCII 65-70
    0-9 are 48-57

  15. #15
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    It's not relevent here where you are told to use if statements, but standard Java types have methods to handle the conversion without the "magic" numbers 65 and 48:

    Java Code:
    remainder = Character.toUpperCase(Character.forDigit(mod, 16));

Similar Threads

  1. One Jspinner modifying the value of the other
    By cotarelo in forum AWT / Swing
    Replies: 1
    Last Post: 06-11-2010, 02:48 PM
  2. Modifying text file
    By right2001 in forum New To Java
    Replies: 7
    Last Post: 04-13-2009, 08:41 AM
  3. Modifying text file
    By right2001 in forum New To Java
    Replies: 7
    Last Post: 04-08-2009, 01:18 AM
  4. image to byte code file conversion
    By nupurashi in forum New To Java
    Replies: 0
    Last Post: 01-28-2009, 10:25 AM
  5. Modifying JSP using IntelliJ IDEA 6.0.5
    By ddeokarb in forum IntelliJ IDEA
    Replies: 0
    Last Post: 11-28-2008, 01:26 PM

Posting Permissions

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