# Octal to Hexadecimal Conversion

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

Example (Octal to Hexadecimal Conversion):

```OCTAL = 1452

The steps required for Octal to Hexadecimal conversion are as follows:

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

#### C++ Program for Octal to Hexadecimal Conversion is as follows:

```/* C++ Program for Octal to Hexadecimal Conversion */
#include<bits/stdc++.h>
using namespace std;
int bin2Dec(string binary)
{
int decimal = 0;
int iterator = 3;
for(int i = 0; i < 4; 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 Octal Number */
string Octal;
cout<<"Enter the Octal Number: ";
cin>>Octal;

/* Converting Octal to hexadecimal*/
string octal_binary;
/* Converting each digit of octal number
to 3-digit binary number and store the
combined result into 'octal_binary'
*/
for(int i = 0; i < Octal.length(); i++)
{
string temp;
switch(Octal[i])
{
case '0': temp = "000";
break;
case '1': temp = "001";
break;
case '2': temp = "010";
break;
case '3': temp = "011";
break;
case '4': temp = "100";
break;
case '5': temp = "101";
break;
case '6': temp = "110";
break;
case '7': temp = "111";
break;
}
/* Combining the 3-digit binary numbers */
octal_binary = octal_binary + temp;
}

/* Length of 'octal_binary' must be multiple of 4 */
if(octal_binary.length() % 4 != 0)
{
/* Inserting Extra 0s, if needed */
while(octal_binary.length() % 4 != 0)
{
octal_binary.insert(0,"0");
}
}

/* Distribute 'octal_binary' into pairs of 4-digit,
convert them into decimal equivalent and then
*/
for(int i = 0; i < octal_binary.length(); i=i+4)
{
string temp = "";
temp+= octal_binary[i];
temp+= octal_binary[i+1];
temp+= octal_binary[i+2];
temp+= octal_binary[i+3];
/* Converting 'temp' into decimal */
int decimal = bin2Dec(temp);
/* Converting decimal to corresponding Hexadecimal */
switch(decimal) {
case 0 :
break;
case 1 :
break;
case 2 :
break;
case 3 :
break;
case 4 :
break;
case 5 :
break;
case 6 :
break;
case 7 :
break;
case 8 :
break;
case 9 :
break;
case 10 :
break;
case 11 :
break;
case 12 :
break;
case 13 :
break;
case 14 :
break;
case 15 :
break;
}
}

/* Truncating Extra 0s from beginning, if any */
{
}

/*Printing the result */
```OUTPUT: