# Designing Code to Convert the base of a Number to any other base in Number System

In this article we will discuss how to design a Class that represents the Number in a Number System and
also provides the facility to convert the BaseÂ associated with that Number i.e. conversion from any Base to any other base like,Â octal to hexa or hexa to decimal etc.

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

Services/ Functions of this class,

int getDecimalValue() :
It will convert the internal number to decimal base and returns the number according to decimal number system.

bool convertBase(int newBase) :

This API will convert the existing base inÂ Number system to any other base of internal number.
It will internally convert the number to decimal base and then convert it to specified base.

Like if internall number is 75 in Base – 8 and convertBase(2) is called,
Then it will first internally convert it to decimal base i.e. 61Â and then finally convert it to binary i.e. 111101 and make the Base to 2 and number string as 111101.

It provides support till Base 35.

Let’s see the code of Number class,

```#include<iostream>
#include<string>
#include<algorithm>

/*
This Class represents the Number in a Number System and
also provides the facility to convert the Number system
associated with that Number i.e. conversion from any Base
to any other base like, octal to hexa or hexa to decimal etc.
*/
struct Number
{
// Contains the number as string
std::string m_data;
// Contains the internal base
int m_base;
// Calculates the power of given number
int powerOf(int val, int power)
{
if(power == 0)
return 1;
int result = val;
for(int i = 1; i < power; i++ )
{
result = result*val;
}

return result;

}
public:
Number(std::string num, int base = 10) :
m_base(base), m_data(num)
{}
int getBase() const {
return m_base;
}

const std::string& getData() const {
return m_data;
}
//  Convert the internal number to decimal base and returns the number
//	according to decimal number system.
int getDecimalValue()
{

int decValue = 0;
for(int i = m_data.size() - 1; i >=0 ; i--)
{
if(m_data[i] >= 'A' && m_data[i] < 'Z')
decValue = decValue + (m_data[i] - 'A' + 10)*powerOf(m_base, (m_data.size() - 1 - i) );
else
decValue = decValue + (m_data[i] - '0')*powerOf(m_base, (m_data.size() - 1 - i) );
}
return decValue;
}
//  This API will convert the existing base in Number system to any other base
bool convertBase(int newBase)
{
if(newBase > (10 + 'Z' - 'A') )
{
std::cout<<"Conversion Not Possible for Base Greater Than "<< (10 + 'Z' - 'A')<<std::endl;
return false;
}
int decValue = getDecimalValue();
std::string newValue = "";
while(decValue > 0)
{
int rem =decValue % newBase;
decValue = decValue / newBase;
char newChar = '0' + rem;
if(newChar > '9')
{
newChar = 'A' + (newChar - '9') - 1;
}
newValue += newChar;
}
std::reverse(newValue.begin(), newValue.end());
m_data= newValue;
m_base = newBase;
return true;
}
};
```

Now let’s see how to use this class,

```int main()
{
Number numObj("75", 8);
std::cout<<numObj.getData()<<" :: "<<numObj.getBase()<<" :: DECIMAL VALUE = "<<numObj.getDecimalValue()<<std::endl;
numObj.convertBase(2);
std::cout<<numObj.getData()<<" :: "<<numObj.getBase()<<" :: DECIMAL VALUE = "<<numObj.getDecimalValue()<<std::endl;
numObj.convertBase(90);
std::cout<<numObj.getData()<<" :: "<<numObj.getBase()<<" :: DECIMAL VALUE = "<<numObj.getDecimalValue()<<std::endl;

return 0;
}
```

Output is as follows,

75 :: 8 :: DECIMAL VALUE = 61
111101 :: 2 :: DECIMAL VALUE = 61
Conversion Not Possible for Base Greater Than 35
111101 :: 2 :: DECIMAL VALUE = 61

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top