Home »
C++ Programs
C++ program to convert hexadecimal to octal number using class
Submitted by Shubh Pachori, on August 25, 2022
Problem statement
Given a hexadecimal value, we have to convert it into an octal value using the class and object approach.
Example:
Input:
Enter Hexadecimal: 23e
Output:
Octal Number: 32476
C++ code to convert hexadecimal to octal number using the class and object approach
#include <iostream>
#include<math.h>
using namespace std;
// create a class
class HexaToOctal {
// private data member
private:
char hexa[20];
// public functions
public:
// getHexa() function to get the hexadecimal
void getHexa() {
cout << "Enter HexaDecimal:";
cin >> hexa;
}
// HToO() function to convert hexadecimal to octal
int HToO() {
// initialising int type variables for operations
int index, length, decimal = 0, octal = 0;
// for loop to find out length of the char array of hexa
for (length = 0; hexa[length]; length++);
// for loop to convert hexadecimal to decimal number
for (index = 0; hexa[index]; index++, length--) {
// if condition to check if the character
// at index is a digit
if (hexa[index] >= '0' && hexa[index] <= '9') {
// if it is a digit then power of 16 and
// length - 1 is multiplied to the digit at
// index in hexa after subtracting 0 from it
decimal = decimal + (hexa[index] - '0') * pow(16, length - 1);
}
// if condition to check if it is a
// uppercase alphabet between A and F
if (hexa[index] >= 'A' && hexa[index] <= 'F') {
// if it is a uppercase alphabet between A and F then
// power of 16 and length - 1 is multiplied to the alphabet
// at index in hexa after subtracting 55 from it
decimal = decimal + (hexa[index] - 55) * pow(16, length - 1);
}
// if condition to check if it is a
// lowercase alphabet between a and f
if (hexa[index] >= 'a' && hexa[index] <= 'f') {
// if it is a lowercase alphabet between a and f then
// power of 16 and length - 1 is multiplied to the alphabet
// at index in hexa after subtracting 87 from it
decimal = decimal + (hexa[index] - 87) * pow(16, length - 1);
}
}
// Now, variable decimal contains the decimal value
// of given hexadecimal number
// reinitialising index with 1
index = 1;
// while loop to convert decimal to octal number
while (decimal != 0) {
// multiply index to the divided decimal by
// 8 and add it to the octal
octal = octal + (decimal % 8) * index;
// divide decimal by 8
decimal = decimal / 8;
// multiply index by 10 at every iteration
index = index * 10;
}
// returning octal number
return octal;
}
};
int main() {
// create an object
HexaToOctal H;
// int type variable to store octal number
int octal;
// function is called by the object to get hexadecimal
H.getHexa();
// HToO() function to convert
octal = H.HToO();
cout << "Octal Number: " << octal;
return 0;
}
Output
RUN 1:
Enter HexaDecimal:12
Octal Number: 22
RUN 2:
Enter HexaDecimal:123af
Octal Number: 221657
Explanation
In the above code, we have created a class HexaToOctal, one char type array data member hexa[20] to store the hexadecimal, and public member functions getHexa() and HToO() to store and convert the given hexadecimal to octal number.
In the main() function, we are creating an object H of class HexaToOctal, reading the hexadecimal given by the user using getHexa() function, and finally calling the HToO() member function to convert the given hexadecimal. The HToO() function contains the logic to convert the given hexadecimal number to an octal number and printing the result.
C++ Class and Object Programs (Set 2) »