Results 1 to 15 of 15
 10242010, 07:49 PM #1Member
 Join Date
 Oct 2010
 Posts
 7
 Rep Power
 0
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); } }
Thank you for your help.
 10242010, 08:16 PM #2Senior Member
 Join Date
 Oct 2010
 Location
 Germany
 Posts
 787
 Rep Power
 6
try:
int remainder; > char remainder;
remainder = value % n; > remainder = (char) ((value % n) > 9 ? ((value % n) % 10) + 65 : (value % n) + 48);
D:D
 10242010, 08:18 PM #3Member
 Join Date
 Oct 2010
 Posts
 7
 Rep Power
 0
Thanks for the help, but the instructions say to use if statements.
 10242010, 08:21 PM #4
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 :)
 10242010, 08:22 PM #5Member
 Join Date
 Oct 2010
 Posts
 7
 Rep Power
 0
care to give an example as to how I would write this?
 10242010, 08:24 PM #6
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 :)
 10242010, 08:25 PM #7Member
 Join Date
 Oct 2010
 Posts
 7
 Rep Power
 0
Thanks. I'll see how I write this.
 10242010, 08:36 PM #8Member
 Join Date
 Oct 2010
 Posts
 7
 Rep Power
 0
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)
 10242010, 09:25 PM #9
 10242010, 09:30 PM #10Member
 Join Date
 Oct 2010
 Posts
 7
 Rep Power
 0
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); } }

Hm, what if you swap your String concatenation:
Java Code:answer = "F" + answer;
 10242010, 09:42 PM #12Member
 Join Date
 Oct 2010
 Posts
 7
 Rep Power
 0
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; 10242010 at 10:28 PM.
 10252010, 05:50 AM #14Senior Member
 Join Date
 Oct 2010
 Location
 Germany
 Posts
 787
 Rep Power
 6
only a small note:
the ternary operator is a shorthand for an ifthenelse 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); }
AF are ASCII 6570
09 are 4857
 10252010, 06:59 AM #15Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,606
 Rep Power
 12
Similar Threads

One Jspinner modifying the value of the other
By cotarelo in forum AWT / SwingReplies: 1Last Post: 06112010, 02:48 PM 
Modifying text file
By right2001 in forum New To JavaReplies: 7Last Post: 04132009, 08:41 AM 
Modifying text file
By right2001 in forum New To JavaReplies: 7Last Post: 04082009, 01:18 AM 
image to byte code file conversion
By nupurashi in forum New To JavaReplies: 0Last Post: 01282009, 11:25 AM 
Modifying JSP using IntelliJ IDEA 6.0.5
By ddeokarb in forum IntelliJ IDEAReplies: 0Last Post: 11282008, 02:26 PM
Bookmarks