# Hexadecimal to Octal Conversion

“** Hexadecimal to Octal Conversion**” is a one of the classic programming problem exercise. Here, we are given a hexadecimal number, entered by user and our task is to write a program to convert the given hexadecimal number to its equivalent octal number.

*Example:*

HEXADECIMAL = 32A OCTAL = 1452

*The steps required to convert Hexadecimal number to Octal number are as follows:*

- Scan the hexadecimal number from left to right digit by digit.
- Convert each digit of hexadecimal number into its corresponding 4-digit binary number, combine them and store it into temporary variable ‘hexadecimal_binary’.
- Make sure the number of digits of ‘hexadecimal_binary’ are divisible by 3. If not, add extra remaining 0s in the front of ‘hexadecimal_binary’.
- Now, distribute the ‘hexadecimal_binary’ into pairs of 3-digit binary numbers.
- Then, convert each 3-digit pair into corresponding Octal number.
- Combine the character and get the result.
- Print the result.

*C++ Program to convert the hexadecimal to octal is as follows:*

/* C++ Program to Convert Hexadecimal Number to Octal Number */ #include<bits/stdc++.h> using namespace std; int bin2Dec(string binary) { int decimal = 0; int iterator = 2; for(int i = 0; i < 3; i++) { char digit = binary[i]; if(digit == '1') decimal = decimal + (1 * pow(2,iterator)); /* Putting it into formula */ iterator--; } return decimal; } int main() { /* Scan the hexadecimal Number */ string Hexadecimal; cout<<"Enter the Hexadecimal Number: "; cin>>Hexadecimal; /* Converting Hexadecimal to octal*/ string hexadecimal_binary; /* Converting each digit of hexadecimal number to 4-digit binary number and store the combined result into 'hexadecimal_binary' */ for(int i = 0; i < Hexadecimal.length(); i++) { string temp; switch(Hexadecimal[i]) { case '0': temp = "0000"; break; case '1': temp = "0001"; break; case '2': temp = "0010"; break; case '3': temp = "0011"; break; case '4': temp = "0100"; break; case '5': temp = "0101"; break; case '6': temp = "0110"; break; case '7': temp = "0111"; break; case '8': temp = "1000"; break; case '9': temp = "1001"; break; case 'A': temp = "1010"; break; case 'B': temp = "1011"; break; case 'C': temp = "1100"; break; case 'D': temp = "1101"; break; case 'E': temp = "1110"; break; case 'F': temp = "1111"; } /* Combining the 3-digit binary numbers */ hexadecimal_binary = hexadecimal_binary + temp; } /* Length of 'hexadecimal_binary' must be multiple of 3 */ if(hexadecimal_binary.length() % 3 != 0) { /* Inserting Extra 0s in front, if needed */ while(hexadecimal_binary.length() % 3 != 0) { hexadecimal_binary.insert(0,"0"); } } /* Distribute 'hexadecimal_binary' into pairs of 3-digit, convert them into decimal equivalent which will be octal equivalent */ string Octal; for(int i = 0; i < hexadecimal_binary.length(); i=i+3) { string temp = ""; temp+= hexadecimal_binary[i]; temp+= hexadecimal_binary[i+1]; temp+= hexadecimal_binary[i+2]; /* Converting 'temp' into decimal */ int decimal = bin2Dec(temp); /* Converting decimal to corresponding Hexadecimal */ switch(decimal) { case 0 : Octal = Octal + '0'; break; case 1 : Octal = Octal + '1'; break; case 2 : Octal = Octal + '2'; break; case 3 : Octal = Octal + '3'; break; case 4 : Octal = Octal + '4'; break; case 5 : Octal = Octal + '5'; break; case 6 : Octal = Octal + '6'; break; case 7 : Octal = Octal + '7'; break; } } /* Truncating Extra 0s from beginning, if any */ while(Octal[0]=='0') { Octal.erase(Octal.begin()+0); } /*Printing the result */ cout<<"The Octal of "<<Hexadecimal<<" is "<<Octal; }

Enter the Hexadecimal number: 32A The Octal of 32A is 1452OUTPUT:

**Related Posts:**

**Program to convert hexadecimal number to binary number.****Program to convert hexadecimal number to decimal number.****Program to check Leap Year.****Program to convert binary number to octal number.****Program to convert binary number to decimal number.****Program to convert binary number to hexadecimal number.****Program to convert octal number to binary number.****Program to convert octal number to decimal number.****Program to convert octal number to hexadecimal number.****Program to convert decimal number to binary number.****Program to convert decimal number to octal number.****Program to convert decimal number to hexadecimal number.****Program to find and print nth Fibonacci number.****Program to find Power of the Number.****Program to find Quotient and Remainder.****Program to find largest amongst three numbers.****Program to find factorial of a number.****Program to find GCD of two numbers.****Program to find LCM of two numbers.****Program to check whether entered number is odd or even.**