# JAVA Program to Convert Octal to Hexadecimal

Octal Number − Octal number is also one of the number systems available. The octal number is represented with 8 digits which is from 0 to 7(0, 1, 2, 3... 7). The Octal numbers are expressed as base-8 in the numeral system.

Hexadecimal Number − Hexadecimal number is also one of the number systemsavailable. The Hexadecimal number is represented with 16 digits which is from 0 to 15(0, 1, 2, 3... 15). From 10 to 15 it is represented as A to F. The Hexadecimal numbers are expressed as base-16 in the numeral system.

Here we first convert the octal number into its appropriate decimal number, and then we again convert that decimal number into hexadecimal number. By this way we convert the Octal number into Hexadecimal number.

## To show you some instances −

### Instance-1

Input octal number is 1232.
The decimal converted value of it = 666.
Now the hexadecimal value of 666 = 29A.


### Instance-2

Input octal number is 5454.
The decimal converted value of it = 2860.
Now the hexadecimal value of 2860= B2C.


### Instance-3

Input octal number is 76564.
The decimal converted value of it = 32116.
Now the hexadecimal value of 32116 = 7D74.


### Algorithm

Step-1 − Get the input number either by static input or user input method.

Step-2 − First we convert the given octal number into decimal number by using ‘Integer.parseInt(octalNumber, 8)’ method.

Step-3 − Then we convert that decimal number into a hexadecimal number by using ‘Integer.toHexString(decimalNumber)’ method.

Step-4 − After getting the hexadecimal value we print that value as output.

### Multiple Approaches

We have provided the solution in different approaches.

• By User Defined Method with Static Input Values.

• By User Defined Method with User Input Values.

Let’s see the program along with its output one by one.

## Approach-1: By Using User Defined Method with Static Input Value

In this approach, we declare an octal input number as static input and pass this number as a parameter in a user defined method, then inside the method by using the algorithm we can convert the octal number into binary number.

## Example

public class Main {
public static void main(String args[]){
//declare and store a octal number by static input method
String inputNumber = "7654";
//call the user defined method to give the output
octalToHex(inputNumber);
}
//user defined method to convert the octal number into hexadecimal number
public static void octalToHex(String octalNumber){
//declare a variable
//Convert the given Octal number to Decimal number and store it into that variable
int decimalNumber = Integer.parseInt(octalNumber, 8);
//declare a variable
//now convert Decimal number to Hexadecimal number and store it into that variable
System.out.print("The Hexadecimal Value of "+ octalNumber + " is " + hexadecimalNumber);
}
}


## Output

The Hexadecimal Value of 7654 is fac


## Approach-2: By Using User Defined Method with Static Input Value.

In this approach, we declare an octal input number by user input and pass these numbers as parameters in a user defined method, then inside the method by using the algorithm we can convert the octal number into hexadecimal number.

## Example

import java.util.Scanner;
public class Main {
public static void main(String args[]){
//create object of scanner class
Scanner sc = new Scanner(System.in);
//ask the user to enter an octal number
System.out.print("Enter an Octal Number = ");
//declare and store an octal number by user input method
String inputNumber = sc.nextLine();
//call the user defined method to give the output
octalToHex(inputNumber);
}
//user defined method to convert the octal number into hexadecimal number
public static void octalToHex(String octalNumber) {
//declare a variable
// Convert the given Octal number to Decimal number and store it into that variable
int decimalNumber = Integer.parseInt(octalNumber, 8);
//declare a variable
// now convert Decimal number to Hexadecimal number and store it into that variable
System.out.print("The Hexadecimal Value of "+ octalNumber + " is " + hexadecimalNumber);
}
}


## Output

Enter an Octal Number = 45342
The Hexadecimal Value of 45342 is 4ae2


In this article, we explored how to convert an octal number to hexadecimal number in Java by using different approaches.