w3resource

Java: Convert a octal number to a hexadecimal number

Java Basic: Exercise-27 with Solution

Octal to Hexadecimal Converter

Write a Java program to convert a octal number to a hexadecimal number.

Octal number: The octal numeral system is the base-8 number system, and uses the digits 0 to 7.

Hexadecimal number: This is a positional numeral system with a radix, or base, of 16. Hexadecimal uses sixteen distinct symbols, most often the symbols 0-9 to represent values zero to nine, and A, B, C, D, E, F (or alternatively a, b, c, d, e, f) to represent values ten to fifteen.

Test Data:
Input any octal number: 100

Pictorial Presentation: Octal to Hexadecimal number

Java: Convert a octal number to a hexadecimal number

Sample Solution:

Java Code:

public class Exercise27 {
    public static void main(String args[]) {
        // Declare variables to store octal number and its decimal and hexadecimal equivalents
        String octal_num, hex_num;
        int decnum;
        
        // Create a Scanner object to read input from the user
        Scanner in = new Scanner(System.in);
        
        // Prompt the user to input an octal number
        System.out.print("Input an octal number : ");
        octal_num = in.nextLine();
        
        // Convert the octal number to its decimal equivalent using parseInt
        decnum = Integer.parseInt(octal_num, 8);
        
        // Convert the decimal number to its hexadecimal equivalent
        hex_num = Integer.toHexString(decnum);
        
        // Display the equivalent hexadecimal number
        System.out.print("Equivalent hexadecimal number: " + hex_num + "\n");
    }
}

Explanation:

In the exercise above -

  • First, it takes an octal number as input from the user using the "Scanner" class and stores it as a string in the variable 'octal_num'.
  • It uses the "Integer.parseInt()" method to convert the 'octal_num' string to an integer 'decnum', specifying base 8 (octal) as the input radix.
  • It then uses the "Integer.toHexString()" method to convert the 'decnum' to its hexadecimal representation and stores it in the 'hex_num' string.
  • Finally, it prints the hexadecimal representation of the original octal number stored in the 'hex_num' variable.

Sample Output:

Input a octal number : 100                                                                                    
Equivalent hexadecimal number: 40 

Flowchart:

Flowchart: Java exercises: Convert a octal number to a hexadecimal number

Java Code Editor:

Previous: Write a Java program to convert a octal number to a binary number.
Next: Write a Java program to convert a hexadecimal to a decimal number.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/java-exercises/basic/java-basic-exercise-27.php