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 to Octal Conversion):

HEXADECIMAL = 32A
OCTAL = 1452

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

  1. Scan the hexadecimal number from left to right digit by digit.
  2. Convert each digit of hexadecimal number into its corresponding 4-digit binary number, combine them and store it into temporary variable ‘hexadecimal_binary’.
  3. 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’.
  4. Now, distribute the ‘hexadecimal_binary’ into pairs of 3-digit binary numbers.
  5. Then, convert each 3-digit pair into corresponding Octal number.
  6. Combine the character and get the result.
  7. Print the result.

C++ Program for hexadecimal to octal Conversion is as follows:

/* C++ Program for Hexadecimal to Octal Conversion */  
#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;  
}  

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

Related Posts:

  1. Program to convert hexadecimal number to decimal number.
  2. Program to convert hexadecimal number to binary number.
  3. Program to convert binary number to octal number.
  4. Program to convert binary number to decimal number.
  5. Program to convert binary number to hexadecimal number.
  6. Program to convert octal number to binary number.
  7. Program to convert octal number to decimal number.
  8. Program to convert octal number to hexadecimal number.
  9. Program to convert decimal number to binary number.
  10. Program to convert decimal number to octal number.
  11. Program to convert decimal number to hexadecimal number.
  12. Program to find Quotient and Remainder.
  13. Program to find largest amongst three numbers.
  14. Program to find factorial of a number.
  15. Program to find GCD of two numbers.
  16. Program to find LCM of two numbers.
  17. Program to check whether entered number is odd or even.
  18. Program to check whether entered number is prime number or not.
  19. Program to check whether entered number is palindrome or not.
  20. Program to check whether entered number is Armstrong Number or Not.

You may also like...