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 = 1452
HEXADECIMAL = 32A

The steps required to convert Octal number to Hexadecimal number 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 to Convert Octal Number to Hexadecimal Number is as follows:

/* C++ Program to Convert Octal Number to Hexadecimal Number */  
#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  
       Hexadecimal equivalent 
    */  
    string Hexadecimal;  
    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 :   
                Hexadecimal = Hexadecimal + '0';  
                break;  
            case 1 :   
                Hexadecimal = Hexadecimal + '1';  
                break;  
            case 2 :   
                Hexadecimal = Hexadecimal + '2';  
                break;  
            case 3 :   
                Hexadecimal = Hexadecimal + '3';  
                break;  
            case 4 :   
                Hexadecimal = Hexadecimal + '4';  
                break;  
            case 5 :   
                Hexadecimal = Hexadecimal + '5';  
                break;  
            case 6 :   
                Hexadecimal = Hexadecimal + '6';  
                break;  
            case 7 :   
                Hexadecimal = Hexadecimal + '7';  
                break;  
            case 8 :   
                Hexadecimal = Hexadecimal + '8';  
                break;  
            case 9 :   
                Hexadecimal = Hexadecimal + '9';  
                break;  
            case 10 :   
                Hexadecimal = Hexadecimal + 'A';  
                break;  
            case 11 :   
                Hexadecimal = Hexadecimal + 'B';  
                break;  
            case 12 :   
                Hexadecimal = Hexadecimal + 'C';  
                break;  
            case 13 :   
                Hexadecimal = Hexadecimal + 'D';  
                break;  
            case 14 :   
                Hexadecimal = Hexadecimal + 'E';  
                break;  
            case 15 :   
                Hexadecimal = Hexadecimal + 'F';  
                break;  
        }  
    }  
      
    /* Truncating Extra 0s from beginning, if any */  
    while(Hexadecimal[0]=='0')  
    {  
        Hexadecimal.erase(Hexadecimal.begin()+0);  
    }  
      
    /*Printing the result */  
    cout<<"The Hexadecimal of "<<Octal<<" is "<<Hexadecimal;  
}  

OUTPUT:
Enter the Octal Number: 222
The Hexadecimal of 222 is 92

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *