In the previous article, we have discussed Java Program for Octal to Binary

In this article we will see how to convert Octal to Hexadecimal.

## Program to Convert Octal to Hexadecimal

So, before going into the program directly, let’s know about octal and hexadecimal.

**Octal Number :**

- The number system with base 8 is generally called octal number system .
- This number system us usually consists of 8 digits i.e. 0,1,2,3,4,5,6,7
- Example – (156)
_{8 }where “8” represent the base and “156” represent the octal - But (186)
_{8 }will be a wrong representation because the digits are possible between 0 to 7.

**Hexadecimal Number :**

- The number system with base 16 is generally called Hexadecimal number system .
- This number system us usually consists of 16 digits i.e. 0,1,2,3,4,5,6,7,8,9 and A,B,C,D,E,F
- Example – (19F)
_{16 }where “16” represent the base and “19F” represent the octal number. - But (18H)
_{16 }will be a wrong representation because the digits are possible between 0 to 9 and A to F.

Lets take a example (545)_{8 } when we convert it to hexadecimal it will be as follows ,

(545)_{8 } = (5 × 8²) + (4 × 8¹) + (5 × 8⁰) = 35

= 5 x 64 + 4 x 8 + 5 x 1

= 320 + 32 +5

= (357)_{10}

_{ }Then , (357)_{10 }= (165)_{16}

While converting to the octal to hexadecimal we can convert octal to decimal then decimal will be converted to hexadecimal.

Let’s see the method to convert Octal to Hexadecimal.

Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.

### Method 1 : Java Program for Octal to Hexadecimal Using Built in functions

**Approach :**

- Take a octal value as input .
- Convert it into its
`decimal`

value by using`Integer.parseInt(input value , 8)`

Store it into a variable . - Convert that variable to hexadecimal using
`Integer.toHexString( )`

store that value into a variable`output`

. - Print the result.

**Program :**

Let’s see the programto understand it more clearly.

import java.util.Scanner; public class Main { public static void main(String[] args) { // creating scanner object Scanner sc = new Scanner(System.in); // input an octal value as a string through scanner class System.out.println("Enter a octal Value : "); String input1=sc.next(); int octal = Integer.parseInt(input1, 8); String output = Integer.toHexString(octal); System.out.println("Converted hexadecimal is :"+output); } }

Output : Enter a octal Value : 545 Converted hexadecimal is :165

**Related Java Programs:**