The ConvertToHex
class contains a program that converts an integer value entered by the user into its equivalent hexadecimal value. Here is a detailed explanation of the code:
Scanner
class from the java.util
package to allow user input from the keyboard.ConvertToHex
has a main
method where the program logic is defined.num
to store the number entered by the user.Scanner
object s
to obtain input from the keyboard using the System.in
stream.System.out.print
method.nextInt()
method of the Scanner
class to store the user’s input in the num
variable.Scanner
object to free up system resources.toHexString
method of the Integer
class to convert the num
value into its hexadecimal representation, which is stored in the hex
string.System.out.println
method to print the result on the console. The result consists of the message “Hexadecimal value of number ” followed by the decimal value of num
, followed by the message ” is: ” followed by the hexadecimal value stored in the hex
string.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