# Thread: Need help modifying code for hex conversion

1. Member 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);
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;
}
}
}```
I do not need a complete rewrite, but an addon to the code to allow the conversion.  Reply With Quote

2. Senior Member  Join Date
Oct 2010
Location
Germany
Posts
785
Rep Power
11

## try:

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

3. Member Join Date
Oct 2010
Posts
7
Rep Power
0

## Thanks for the help, but the instructions say to use if statements.  Reply With Quote

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.  Reply With Quote

5. Member Join Date
Oct 2010
Posts
7
Rep Power
0

## care to give an example as to how I would write this?  Reply With Quote

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)  Reply With Quote

7. Member Join Date
Oct 2010
Posts
7
Rep Power
0

## Thanks. I'll see how I write this.  Reply With Quote

8. Member 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)  Reply With Quote

9. ## What is the code you are currently using?  Reply With Quote

10. Member 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);
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)
if (remainder == 11)
if (remainder == 12)
if (remainder == 13)
if (remainder == 14)
if (remainder == 15)
}
}
}```  Reply With Quote

11. ## 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?  Reply With Quote

12. Member Join Date
Oct 2010
Posts
7
Rep Power
0

## well not really. We haven't gone over this in lecture yet.  Reply With Quote

13. ##  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.
Last edited by Fubarable; 10-24-2010 at 10:28 PM.  Reply With Quote

14. Senior Member  Join Date
Oct 2010
Location
Germany
Posts
785
Rep Power
11

##  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
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  Reply With Quote

15. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## 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));`  Reply With Quote

#### Posting Permissions

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