C++ program to convert decimal number to hexadecimal using class

Given a decimal number, we have to convert it into hexadecimal using the class and object approach.
Submitted by Shubh Pachori, on August 25, 2022

Example:

Input:
Enter Decimal Number: 22

Output:
Hexadecimal: 16

C++ code to convert decimal number to hexadecimal using the class and object approach

#include <iostream>
using namespace std;

// create a class
class DecimalToHexa {
  // private data member
  private:
    int decimal;

  // public functions
  public:
    // getDecimal() function to get the Decimal Number
    void getDecimal() {
      cout << "Enter Decimal Number:";
      cin >> decimal;
    }

  // DToH() function to convert decimal to hexadecimal
  string DToH() {
    // char type array to store hexadecimal value
    char hexa[20];
    char temp[20];

    // counters for hexadecimal number array
    int index_1 = 0, index_2, index_3;

    // while loop to convert decimal number 
    // to hexadecimal
    while (decimal) {

      // int type variable to store remaining 
      // of decimal
      int remain = 0;

      // storing remainder in remain variable.
      remain = decimal % 16;

      // check if remain < 10
      if (remain < 10) {
        // assigning remain with a addition of 48 
        // in it to the index_1 of temp
        temp[index_1] = remain + 48;

        // increment of 1 in index_1
        index_1++;
      }

      // else condition if remain is not smaller than 10
      else {
        // assigning remain with a addition of 55 
        // in it to the index_1 of temp
        temp[index_1] = remain + 55;

        // increment of 1 in index_1
        index_1++;
      }

      // divide decimal by 16
      decimal = decimal / 16;
    }

    // for loop to reverse the temp and store it in hexa
    for (index_2 = index_1 - 1, index_3 = 0; index_2 >= 0; index_2--, index_3++) {
      // copying values of temp in hexa
      hexa[index_3] = temp[index_2];
    }

    // transfering null in the hexa
    hexa[index_3] = 0;

    // returning hexa
    return hexa;
  }
};

int main() {
  // create an object
  DecimalToHexa D;

  // string type variable to store hexadecimal
  string hexa;

  // function is called by the object 
  // to get decimal number
  D.getDecimal();

  // DToH() function to convert decimal 
  // to hexadecimal
  hexa = D.DToH();

  cout << "HexaDecimal : " << hexa;

  return 0;
}

Output:

Enter Decimal Number: 6655
Hexadecimal: 19FF

Explanation:

In the above code, we have created a class DecimalToHexa, one int type data member decimal to store the decimal number, and public member functions getDecimal() and DToH() to store and convert the given decimal number to hexadecimal.

In the main() function, we are creating an object D of class DecimalToHexa, reading the decimal number given by the user using getDecimal() function, and finally calling the DToH() member function to convert the given decimal to hexadecimal. The DToH() function contains the logic to convert the given decimal number to hexadecimal and printing the result.

C++ Class and Object Programs (Set 2) »





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.