Java Program to Convert Hexadecimal to Octal

Program to convert hexadecimal to octal

In this article we will see how to convert Binary 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” represents the base and “156” represents 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” represents the octal number.
  • But (18H)16 will be a wrong representation because the digits are possible between 0 to 9 and A to F.

When we are converting the hexadecimal number to octal we follow the below steps

  • Convert the hexadecimal number to the decimal number .
  • Convert the decimal number to the equivalent octal number .

Lets take a example  say (4AB)16  is a hexadecimal number that we want to convert it to equivalent octal then it will be as follows .

(4AB)16  = (4 × 16²) + (10 × 16¹) + (11 × 16⁰)

= 4 x 256 + 10 x 16 + 11 x 1

= 1024 + 160 +11

=(1195)10

                    (1195)10  can be converted into octal and the value will be 2253

Which can be again represented as (2253)8. 

Let’s see different ways to do it.

METHOD 1 : Using inbuilt function

Approach:

  • Take a hexadecimal value form the input.
  • Convert it into its decimal value by using Integer.parseInt(input value , 16) Store it into a variable.
  • Convert that variable to octal using Integer.toOctalString( ) store that value into a variable output.
  • Print the result.

Program:

Let’s see the program to 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 a hexadecimal value as a string through scanner class 
        System.out.println("Enter a hexadecimal  Value : ");
        String input1=sc.next();
        int octal = Integer.parseInt(input1, 16);
        String output = Integer.toOctalString(octal);
        System.out.println("Converted octal is :"+output);
    }
}
 
Output :

Enter a hexadecimal  Value : 4AB
Converted octal is :2253

Method 2 : Using switch case

Approach:

  • Take a hexadecimal value as the input.
  • Take each possible value with the help of switch case calculate its decimal equivalent of each digits.
  • Add it each time and Store it into a variable lets say decimal.
  • Take for loop by divide each time with 8 and taking all reminder and store it in a character array.
  • Combine all element in reverse order with a sting called output.
  • Print the result.

Program:

Let’s see the program to 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 a  octal value as a string  through scanner class 
        System.out.println("Enter a hexadecimal  Value : ");
        String input1=sc.next();
        int len = input1.length() - 1;
        int decimal=0;
    // finding the decimal equivalent of the hexa decimal number
    for(int a = 0; a < input1.length() ; a ++ )
    {
        char ch = input1.charAt(a);
        switch (ch)
        {
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                decimal = decimal + Integer.parseInt(Character.toString(ch))*
                                                (int)Math.pow(16,len);
                len--;
                break;
            case 'a':
            case 'A':
                decimal = decimal + 10 * (int)Math.pow(16,len);
                len--;
                break;
            case 'b':
            case 'B':
                decimal = decimal + 11 * (int)Math.pow(16,len);
                len--;
                break;
            case 'c':
            case 'C':
                decimal = decimal + 12 * (int)Math.pow(16,len);
                len--;
                break;
            case 'd':
            case 'D':
                decimal = decimal + 13 * (int)Math.pow(16,len);
                len--;
                break;
            case 'e':
            case 'E':
                decimal = decimal + 14 * (int)Math.pow(16,len);
                len--;
                break;
            case 'f':
            case 'F':
                decimal = decimal + 15 * (int)Math.pow(16,len);
                len--;
                break;
            default:
                System.out.println("Invalid input");
                break;
        }
    }
    // String store the octal equivalent of  hexadecimal number.
    String output ="";
    //converting decimal to octal number.
    while(decimal > 0)
    {
        output = decimal % 8 + output;
        decimal = decimal / 8;
    }
        System.out.println("Converted octal is :"+output);
    }
}
Output :

Enter a hexadecimal  Value : 4AB
Converted octal is :2253

Leave a Comment