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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/exceptions("`Exceptions`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/booleans("`Booleans`") java/BasicSyntaxGroup -.-> java/break_continue("`Break/Continue`") java/BasicSyntaxGroup -.-> java/comments("`Comments`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/if_else("`If...Else`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/BasicSyntaxGroup -.-> java/while_loop("`While Loop`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117922{{"`Java Long toHexString Method`"}} java/classes_objects -.-> lab-117922{{"`Java Long toHexString Method`"}} java/class_methods -.-> lab-117922{{"`Java Long toHexString Method`"}} java/exceptions -.-> lab-117922{{"`Java Long toHexString Method`"}} java/modifiers -.-> lab-117922{{"`Java Long toHexString Method`"}} java/oop -.-> lab-117922{{"`Java Long toHexString Method`"}} java/user_input -.-> lab-117922{{"`Java Long toHexString Method`"}} java/wrapper_classes -.-> lab-117922{{"`Java Long toHexString Method`"}} java/identifier -.-> lab-117922{{"`Java Long toHexString Method`"}} java/arrays -.-> lab-117922{{"`Java Long toHexString Method`"}} java/booleans -.-> lab-117922{{"`Java Long toHexString Method`"}} java/break_continue -.-> lab-117922{{"`Java Long toHexString Method`"}} java/comments -.-> lab-117922{{"`Java Long toHexString Method`"}} java/data_types -.-> lab-117922{{"`Java Long toHexString Method`"}} java/if_else -.-> lab-117922{{"`Java Long toHexString Method`"}} java/operators -.-> lab-117922{{"`Java Long toHexString Method`"}} java/output -.-> lab-117922{{"`Java Long toHexString Method`"}} java/strings -.-> lab-117922{{"`Java Long toHexString Method`"}} java/variables -.-> lab-117922{{"`Java Long toHexString Method`"}} java/while_loop -.-> lab-117922{{"`Java Long toHexString Method`"}} java/system_methods -.-> lab-117922{{"`Java Long toHexString Method`"}} end

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