# Java Program to Convert Octal to Hexadecimal

## Program to Convert Octal to 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.

• 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.

### Method 1 : 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);

Output :
Converted hexadecimal is :165