# HexaDecimal to Octal in java

Let’s learn hexadecimal to octal in java.

## Hexadecimal to Octal in java

Hexadecimal number is base 16 number. It contains 0 to 9 A, B, C, D, E, F where F represents 15. While octal number is base 8 number. It contains 0 to 7.

For example: hexadecimal value – 2E5

binary value of 2 is 0010

binary value of E is 1110

binary value of 5 is 0101

001011100101 group this number in terms of three like this,

001 011 100 101

Octal number is – 1345

Let’s see program on hexadecimal to octal.

```import java.util.Scanner;
{
public static void main(String[] args)
{
int decimalNumber, a = 1, b;
int[] octalNumber = new int[100];
Scanner sc = new Scanner(System.in);
String strHexNumber = sc.nextLine();
// converting hexadecimal number to decimal number
decimalNumber = hexToDecimal(strHexNumber);
// converting decimal to octal
while(decimalNumber != 0)
{
octalNumber[a++] = decimalNumber % 8;
decimalNumber = decimalNumber / 8;
}
System.out.print("Octal number is: ");
for(b = a - 1; b > 0; b--)
{
System.out.print(octalNumber[b]);
}
System.out.print("\n");
sc.close();
}
public static int hexToDecimal(String str)
{
String strDigits = "0123456789ABCDEF";
str = str.toUpperCase();
int value = 0;
for(int a = 0; a < str.length(); a++)
{
char ch = str.charAt(a);
int deci = strDigits.indexOf(ch);
value = 16 * value + deci;
}
return value;
}
}
```

Output: