Java Long toHexString Method

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn how to use the Java toHexString() method, which returns the hexadecimal equivalent of a base 16 of the value passed as an unsigned long. You will learn how to use the toHexString() method, its parameters, and its return type.

Create a Java class

Open a terminal and create a new Java class called LongToHexadecimal using the following command:

touch LongToHexadecimal.java

Open the LongToHexadecimal.java file in a text editor.

Write the main method

Paste the following code to define the main method:

public class LongToHexadecimal {
    public static void main(String[] args){
        // Your code here
    }
}

Convert a positive long to hexadecimal

Next, you will use the toHexString() method to convert a positive long to hexadecimal.

Add the following code inside the main method to convert the long value 60L to its hexadecimal equivalent:

long i = 60L;
System.out.println("Actual number is = " + i);
System.out.println("Hexadecimal equivalent is = " + Long.toHexString(i)); //returns the long value in hexadecimal base 16 as a string

Compile and run the program in the terminal using the following command:

javac LongToHexadecimal.java && java LongToHexadecimal

You should see the following output:

Actual number is = 60
Hexadecimal equivalent is = 3c

Convert a negative long to hexadecimal

Next, you will use the toHexString() method to convert a negative long to hexadecimal.

Add the following code inside the main method to convert the long value -52L to its hexadecimal equivalent:

long j = -52L;
System.out.println("Actual number is = " + j);
System.out.println("Hexadecimal equivalent is = " + Long.toHexString(j)); //returns the long value in hexadecimal base 16 as a string

Compile and run the program again in the terminal using the following command:

javac LongToHexadecimal.java && java LongToHexadecimal

You should see the following output:

Actual number is = -52
Hexadecimal equivalent is = ffffffffffffffcc

Accept input long from the user

Next, you will modify the program to accept input from the user.

Add the following code inside the main method to read a long input from the user and convert it to its hexadecimal equivalent:

Scanner sc = new Scanner(System.in);
System.out.print("Enter the Number = ");
long k = sc.nextLong();
System.out.println("Actual Number is  = " + k);
System.out.println("Hexadecimal representation is = " + Long.toHexString(k)); //returns the long value in hexadecimal base 16 as a string

Compile and run the program again in the terminal using the following command:

javac LongToHexadecimal.java && java LongToHexadecimal

You should see an output like the following:

Enter the Number = 585
Actual Number is  = 585
Hexadecimal representation is = 249

Handle invalid input

Next, you will add code to handle invalid input.

Modify the existing code inside the main method to catch any input exceptions:

Scanner sc = new Scanner(System.in);
try {
    System.out.print("Enter the Number = ");
    long k = sc.nextLong();
    System.out.println("Actual Number is  = " + k);
    System.out.println("Hexadecimal representation is = " + Long.toHexString(k)); //returns the long value in hexadecimal base 16 as a string
} catch (InputMismatchException ex) {
    System.out.println("Invalid input! Please enter a valid long value.");
}

Compile and run the program again in the terminal using the following command:

javac LongToHexadecimal.java && java LongToHexadecimal

You should see an output like the following:

Enter the Number = abc
Invalid input! Please enter a valid long value.

Add a loop

Next, you will modify the program to accept input from the user until they quit the program.

Add the following code inside the main method to keep the program running until the user quits:

Scanner sc = new Scanner(System.in);
while (true) {
    System.out.print("Enter the Number (or -1 to quit) = ");
    long k = sc.nextLong();
    if (k == -1) {
        System.out.println("Goodbye!");
        break;
    }
    System.out.println("Actual Number is  = " + k);
    System.out.println("Hexadecimal representation is = " + Long.toHexString(k)); //returns the long value in hexadecimal base 16 as a string
}

Compile and run the program again in the terminal using the following command:

javac LongToHexadecimal.java && java LongToHexadecimal

You should see an output like the following:

Enter the Number (or -1 to quit) = 456
Actual Number is  = 456
Hexadecimal representation is = 1c8
Enter the Number (or -1 to quit) = -999
Actual Number is  = -999
Hexadecimal representation is = fffffffffffffc19
Enter the Number (or -1 to quit) = 754
Actual Number is  = 754
Hexadecimal representation is = 2f2
Enter the Number (or -1 to quit) = -1
Goodbye!

Test edge cases

Finally, test edge cases where the input number is the largest and smallest number that can be represented by a long.

Add the following inside the while loop in the main method:

System.out.println("Actual Number is  = " + Long.MAX_VALUE);
System.out.println("Hexadecimal representation is = " + Long.toHexString(Long.MAX_VALUE)); //returns the long value in hexadecimal base 16 as a string
System.out.println("Actual Number is  = " + Long.MIN_VALUE);
System.out.println("Hexadecimal representation is = " + Long.toHexString(Long.MIN_VALUE)); //returns the long value in hexadecimal base 16 as a string

Compile and run the program again in the terminal using the following command:

javac LongToHexadecimal.java && java LongToHexadecimal

You should see an output like the following:

Enter the Number (or -1 to quit) = 9223372036854775807
Actual Number is  = 9223372036854775807
Hexadecimal representation is = 7fffffffffffffff
Actual Number is  = -9223372036854775808
Hexadecimal representation is = 8000000000000000
Enter the Number (or -1 to quit) = -1
Goodbye!

Summary

In this lab, you learned how to use the Java toHexString() method to convert a long value to its hexadecimal equivalent. You practiced reading input from the user, handling user input exceptions, and using a loop to keep a program running until the user actively quits. You can use these programming concepts to build more complex Java applications that manipulate long values and take user input.

Other Java Tutorials you may like