C++ Exercises: Convert a hexadecimal number to octal number
81. Hexadecimal to Octal Conversion
Write a C++ program to convert a hexadecimal number to an octal number.
Visual Presentation:

Sample Solution:-
C++ Code :
#include<iostream> // Include necessary libraries
#include<stdlib.h>
#include<math.h>
using namespace std; // Use the standard namespace
// Function to convert hexadecimal to decimal
unsigned long Hex_To_Dec(char hex[]) {
    char *hexstring;
    int length = 0;
    const int hexbase = 16; // Define base 16 for hexadecimal
    unsigned long dnum = 0;
    int i;
    // Calculate the length of the hexadecimal string
    for (hexstring = hex; *hexstring != '\0'; hexstring++) {
        length++;
    }
    hexstring = hex; // Reset the pointer to the start of the hexadecimal string
    // Convert each character of the hexadecimal string to decimal
    for (i = 0; *hexstring != '\0' && i < length; i++, hexstring++) {
        if (*hexstring >= 48 && *hexstring <= 57) {  
            // Convert character to decimal if it's a number (0-9)
            dnum += (((int)(*hexstring)) - 48) * pow(hexbase, length - i - 1);
        }
        else if ((*hexstring >= 65 && *hexstring <= 70)) {  
            // Convert character to decimal if it's an uppercase letter (A-F)
            dnum += (((int)(*hexstring)) - 55) * pow(hexbase, length - i - 1);
        }
        else if (*hexstring >= 97 && *hexstring <= 102) {  
            // Convert character to decimal if it's a lowercase letter (a-f)
            dnum += (((int)(*hexstring)) - 87) * pow(hexbase, length - i - 1);
        }
        else {
            // Display an error message for an invalid input character
            cout<<" The given hexadecimal number is invalid. \n";
        }
    }
    return dnum; // Return the resulting decimal number
}
// Main function
int main() {
    unsigned long dnum;
    char hex[9]; // Array to store the input hexadecimal number
    int dec_num, rem = 1, m, n;
    int oct_num[100], quot;
    dec_num = 0;
    cout << "\n\n Convert any hexadecimal number to octal number:\n"; // Display purpose of the program
    cout << "----------------------------------------------------\n"; // Display separator line
    cout << " Input any 32-bit Hexadecimal Number: ";  
    cin >> hex; // Read the hexadecimal input from the user
    dnum = Hex_To_Dec(hex); // Call the function to convert hexadecimal to decimal
    quot = dnum;
    cout << " The equivalent octal number is: ";
    // Conversion from decimal to octal
    while (quot != 0) {
        oct_num[m++] = quot % 8;
        quot = quot / 8;
    }
    // Display the resulting octal number
    for (n = m - 1; n >= 0; n--) {
        cout << oct_num[n];
    }
    cout << "\n"; // Print a newline
}
Sample Output:
Convert any hexadecimal number to octal number: ---------------------------------------------------- Input any 32-bit Hexadecimal Number: 5f The equivalant octal number is: 137
Flowchart:

For more Practice: Solve these Related Problems:
- Write a C++ program to convert a hexadecimal number to octal by chaining conversions through decimal.
 - Write a C++ program that reads a hexadecimal input and outputs its octal equivalent using intermediate conversion methods.
 - Write a C++ program to compute the octal representation of a hexadecimal number by mapping hex digits to binary then to octal.
 - Write a C++ program that converts a hexadecimal string to octal by first converting to decimal and then formatting the output in octal.
 
Go to:
PREV : Hexadecimal to Binary Conversion.
 NEXT :  Compare Two Numbers.
C++ Code Editor:
Contribute your code and comments through Disqus.
What is the difficulty level of this exercise?
