# Need help modifying code for hex conversion

• 10-24-2010, 07:49 PM
gamer765
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:

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.
• 10-24-2010, 08:16 PM
eRaaaa
try:

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

:D:D:D
• 10-24-2010, 08:18 PM
gamer765
Thanks for the help, but the instructions say to use if statements.
• 10-24-2010, 08:21 PM
joshdgreen
Inside the while loop you can just create 5 separate if statements:

Code:

```if(remainder = 10) if(remainder = 11) etc...```

Then you just need a character to hold the hex value like eRaaaa said.
• 10-24-2010, 08:22 PM
gamer765
care to give an example as to how I would write this?
• 10-24-2010, 08:24 PM
joshdgreen
Here's a little help to get you started, and actually I guess you won't need a char variable:

Code:

```if(remainder == 10)     answer += 'A';```
• 10-24-2010, 08:25 PM
gamer765
Thanks. I'll see how I write this.
• 10-24-2010, 08:36 PM
gamer765
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)
• 10-24-2010, 09:25 PM
Zack
What is the code you are currently using?
• 10-24-2010, 09:30 PM
gamer765
I am using this as my code so far

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);         } }```
• 10-24-2010, 09:37 PM
Fubarable
Hm, what if you swap your String concatenation:

Code:

`answer = "F" + answer;`
but really... you should be able to figure things like this out, right?
• 10-24-2010, 09:42 PM
gamer765
well not really. We haven't gone over this in lecture yet.
• 10-24-2010, 09:46 PM
Fubarable
Quote:

Originally Posted by gamer765
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.
• 10-25-2010, 05:50 AM
eRaaaa
Quote:

Originally Posted by gamer765
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
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
• 10-25-2010, 06:59 AM
pbrockway2
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:

Code:

`remainder = Character.toUpperCase(Character.forDigit(mod, 16));`