A hexadecimal number is used to computer system designers and programmers because they provide a human-friendly representation of binary code. Each hexadecimal digits represents four bits of binary digits. There are 16 symbols in hexadecimal, they are 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F. Where A,B,C,D,E,F are single bit representation of decimal digit 10,11,12,13,14 and 15.

**Representation of Hexadecimal Number**

Each Hexadecimal digits are represented using only 4 bits, each group of bits having a distinct values 0000 and 1111 example F = 15 = 8+4+2+1. The equivalent binary number of Hexadecimal number gives below.

Hexadecimal | Binary | Octal |

0 | 0000 | 0 |

1 | 0001 | 1 |

2 | 0010 | 2 |

3 | 0011 | 3 |

4 | 0100 | 4 |

5 | 0101 | 5 |

6 | 0110 | 6 |

7 | 0111 | 7 |

8 | 1000 | 10 |

9 | 1001 | 11 |

A | 1010 | 12 |

B | 1011 | 13 |

C | 1100 | 14 |

D | 1101 | 15 |

E | 1110 | 16 |

F | 1111 | 17 |

**Method for Hexadecimal to Octal Conversion**

**One of the best may to conversion in electronics and communications. Follow the below steps to perform hexadecimal digits to octal digit conversion:-**

**step 1:**Separate the digits of the given hex number, if it contains more than 1 digit.**step 2:**Find the equivalent binary number for each digit of the octal number. Add 0’s to the left if any of the binary equivalents is shorter than 4 bits.**step 3:**Write the all group’s binary numbers together, maintaining the same group.**step 4:**Separate the binary digits into groups, each containing 3 bits or digits from right to left. Add 0s to the left, if the last group contains less than 3 bits.**step 5:**Find the octal equivalent for each group.**step 6:**Write all octal equivalent of each digit together where keeping the same order provides the octal equivalent for the given hexadecimal.

**Code:-**

import java.util.Scanner; public class sample1 { public static int hexdecimal(String s) { String digits = "0123456789ABCDEF"; s = s.toUpperCase(); int val = 0; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); int d = digits.indexOf(c); val = 16*val + d; } return val; } public static void main(String args[]) { String hexdec_num; int dec_num, i=1, j; int octal_num[] = new int[100]; Scanner in = new Scanner(System.in); System.out.print("Input a hexadecimal number: "); hexdec_num = in.nextLine(); // hexadecimal to decimal dec_num = hexdecimal(hexdec_num); //decimal to octal while(dec_num != 0) { octal_num[i++] = dec_num%8; dec_num = dec_num/8; } System.out.print("Equivalent of octal number is: "); for(j=i-1; j>0; j--) { System.out.print(octal_num[j]); } System.out.print("\n"); } }