Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Members have full access to the forums. Advertisements are removed for registered users.

# Thread: Converting from decimal to hexadecimal. Can't figure out how to output correctly

1. ## Converting from decimal to hexadecimal. Can't figure out how to output correctly

```
import java.util.Scanner;

public class FindHexString {
public static void main(String[] args) {

String hexString = ""; //output a hex string
int decNumber;		//decimal number to be converted
int storeInput; 	//copy f input decimal number
int remainder;

Scanner in = new Scanner(System.in);

//read the decimal number to be converted
System.out.println("Please enter a positive integer: ");
decNumber = in.nextInt();
storeInput = decNumber;		//remember input for late print

//rejecting negative number input
while(decNumber < 0 ){
System.out.println("Enter only positive integers please!");
decNumber = in.nextInt();
storeInput = decNumber;

}

if(decNumber == 0 ){
System.out.println("Input decimal number is: " + storeInput);
System.out.println("Hexadecimal value is: 0");
return;
}

//conversion process
while(decNumber > 0){
remainder = decNumber % 16;
decNumber /= 16;
hexString = remainder + hexString;
}

if(decNumber = 10){              //this is the part I'm having trouble with
hexString = "A";
}

System.out.println("Input decimal number is " + storeInput);
System.out.println("Hexadecimal value = " + hexString);

}
}```

I am trying to figure out how to convert from decimal to hexadecimal without using the java library function. This is just a guess. I could be wrong.
Please and thank you for your help.  Reply With Quote

3. ## Re: Converting from decimal to hexadecimal. Can't figure out how to output correctly

If you just want to print the hexadecimal value corresponding to the decimal then you can use print formats like..
`System.out.printf("%X", integerValue);`
.  Reply With Quote

4. ## Re: Converting from decimal to hexadecimal. Can't figure out how to output correctly Originally Posted by BTroj ```//conversion process
while(decNumber > 0){
remainder = decNumber % 16;
decNumber /= 16;
hexString = remainder + hexString;
}```
If you want to do the conversion yourself (no use of standard/predefined methods, for learning/exercise reasons), that code is not sufficient.
The use of division/modulo operator is ok. But the modulo gives you a value 0...15 and for values in range 10...15 you must translate them to "A"..."F".  Reply With Quote

5. ## Re: Converting from decimal to hexadecimal. Can't figure out how to output correctly

In addition, note the bug at:

`if(decNumber = 10){`

You'd want to use the double-equals equality operator. (Summary of Operators (The Java Tutorials > Learning the Java Language > Language Basics))  Reply With Quote

6. ## Re: Converting from decimal to hexadecimal. Can't figure out how to output correctly

This is an example of the code that i use to convert decimal numbers to hexadecimal numbers. you will need a for loop too.

```int x = 3901;
System.out.println( Integer.toHexString(x) );```  Reply With Quote