To convert a number from the decimal system to the hexadecimal system, the toHexString()
method is used in Java. The Integer
class provides several methods, such as the conversion methods.
import java.util.Scanner;
public class ConvertToHex {
public static void main(String[] args) {
int num;
Scanner s = new Scanner(System.in);
System.out.print("Please enter a number: ");
num = s.nextInt();
s.close();
String hex = Integer.toHexString(num);
System.out.println("Hexadecimal value of number " + num + " is: " + hex);
}
}
Please enter a number: 14
Hexadecimal value of number 14 is: e