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,

Now let’s see how to use this class,

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

Click Here to Subscribe for more Articles / Tutorials like this.