Convert the Base of a Number to any Other Base in Number System

In this article, we will look at how to create a Class that represents a Number in a Number System and also allows you to convert the Base associated with that Number, such as octal to hexa or hexa to decimal.

Create a Number class that contains the number as a string and the corresponding base as an Integer.

This class services/functions, are listed below

1)int getDecimalValue()

It transforms the internal number into a decimal base and returns the number by the system of decimal numbers.

2)bool convertBase(int newBase) :

This API can convert the current base in the Number system to some other internal number base.
It converts the number internally to the decimal base and then converts the number to the designated base.

Like when the internal number is 75, it will be converted to a base of 61 internally first and then to a binary number i.e. 111101 and to a base of 2, and the numbers to 111101, second. This is done by converting the interface into a base of 75 at Base-8.

It provides help up to Base 35.

Below is the implementation:

#include <bits/stdc++.h>
using namespace std;
/*
This class is the number in a numbers system, which
therefore allows the conversion from any base to any base,
e.g. octal or hexa, decimal, etc. of the number-system
associated with that number.
*/
struct Number {
    // it will contain the given number as a string
    string strnum;
    // internal base of number
    int numbase;
    // Calculating the power
    int powerOf(int value, int power)
    {
        if (power == 0)
            return 1;
        int powerres = value;
        for (int i = 1; i < power; i++) {
            powerres = powerres * value;
        }
        return powerres;
    }

public:
    Number(string number, int base = 10)
        : numbase(base)
        , strnum(number)
    {
    }
    int getBase() const { return numbase; }
    const string& getData() const { return strnum; }
    /*Convert the internal number to decimal base and return
    the result using the decimal number scheme.
    */
    int getDecimalValue()
    {
        int decResult = 0;
        for (int i = strnum.size() - 1; i >= 0; i--) {
            if (strnum[i] >= 'A' && strnum[i] < 'Z')
                decResult = decResult+ (strnum[i] - 'A' + 10)* powerOf(numbase,(strnum.size()- 1 - i));
            else
                decResult = decResult+ (strnum[i] - '0')* powerOf(numbase,(strnum.size()- 1 - i));
        }
        return decResult;
    }
    //  This API can convert the current Number system's
    //  base to some other base.
    bool convertBase(int newBase)
    {
        if (newBase > (10 + 'Z' - 'A')) {
            cout << "Conversion is not possible if the "
                    "base is greater than"
                 << (10 + 'Z' - 'A') << std::endl;
            return false;
        }
        int decimalValue = getDecimalValue();
        string newNumValue = "";
        while (decimalValue > 0) {
            int remainder = decimalValue % newBase;
            decimalValue = decimalValue / newBase;
            char newCharacter = '0' + remainder;
            if (newCharacter > '9') {
                newCharacter
                    = 'A' + (newCharacter - '9') - 1;
            }
            newNumValue += newCharacter;
        }
        reverse(newNumValue.begin(), newNumValue.end());
        strnum = newNumValue;
        numbase = newBase;
        return true;
    }
};
int main()
{
    Number numberObject("67", 11);
    cout << numberObject.getData() << " : "
         << numberObject.getBase() << " Decimal Value = "
         << numberObject.getDecimalValue() << endl;
    numberObject.convertBase(2);
    cout << numberObject.getData() << " : "
         << numberObject.getBase() << " Decimal Value = "
         << numberObject.getDecimalValue() << endl;
    numberObject.convertBase(46);
    cout << numberObject.getData() << " : "
         << numberObject.getBase() << " Decimal Value = "
         << numberObject.getDecimalValue() << endl;
    return 0;
}

Output:

67 : 11 Decimal Value = 73
1001001 : 2 Decimal Value = 73
Conversion is not possible if the base is greater than35
1001001 : 2 Decimal Value = 73

Related Programs: