```
#ifndef BASE_H
#define BASE_H
/******************************************************************************
*
* Its my first time commenting my code so I apologize if you don't understand some parts of it.
* I'll be happy if you ask.
*
* I will appreciate it if you give some tips on commenting too.
*
* Thanks
*
* Shayan Javani
*
*///***************************************************************************
#include <math.h>
#include <string>
using namespace std;
//Generates alphabetical digits for the given base
void genDigs(int base,char *digs){
if (base>36){ //Because for bases greater than 36,the capital alphabets are not enough
for(int i=0;i<26;++i) //Generates capital alphabets
digs[i]=i+65;
for(int i=26;i<base-10;++i) //Generates small alphabets
digs[i]=i+71;
}
else
for(int i=0;i<base-10;++i) //Generates capital alphabets for bases smaller than 36
digs[i]=i+65;
}
//Gets the converted number in an int array and returns a string containing the number with proper digits
string toNorm(int *num,int digNum,int base){
char *digs=new char; //An array for keeping digits in given base
genDigs(base,digs);
string Num; //String variable for keeping final form of number
for(int i=0;i<digNum;++i){
if(num[i]<10) //For elemnts smaller than 10,we can use the element itself as digits
Num[i]=num[i]+48;
else
Num[i]=digs[num[i]-10]; //Otherwise it should be replaced by an alphabetical digit
}
delete [] digs;
return Num;
}
//Gets an string as a proper number in the given base and replaces alphabetical digits with numbers
int fromNorm(string num,int base,int *dest){
char *digs=new char[base-10];
genDigs(base,digs);
for(int i=0;i<num.size();++i){
bool found=false;
for(int j=0;j<base-10;++j){
if(num[i]==digs[j]){
dest[i]=j+10; //The index of a digit is its equivalent number minus 10
found=true;
break;
}
}
if(!found) //If the digits is a number smaller than 10
dest[i]=num[i]-48; //Which can be used right after converting it from char to int
}
delete[] digs;
return num.size();
}
//One way of converting decimal numbers to a given base
int DecToB1(unsigned long int num,int base,int *dest){
int digNum=(int)ceil(log(num)/log(base)); //Calculates the number of digits in the given base
for(int i=digNum-1;i>0;--i){
dest[i]=num%base;
num=(unsigned long int)floor(num/base);
}
dest[0]=num;
return digNum;
}
//A function which does the convert and also replaces numbers with proper digits
string DecToB(unsigned long int num,int base,int &digNum){
int *dest=new int;
int n=DecToB1(num,base,dest);
digNum=n;
string Num=toNorm(dest,n,base);
return Num;
}
//Converting numbers from a base to decimal
unsigned long int BToDec(string baseNum,int base){
int *num=new int;
int n=fromNorm(baseNum,base,num);
int Num=0;
for(int i=n;i>0;--i)
Num+=num[n-i]*(unsigned long int)pow(base,i-1);
delete [] num;
return Num;
}
//Another way of converting decimal numbers to other bases which I do
int DecToB2(unsigned long int num,int base,int *dest){
int digNum=(int)ceil(log(num)/log(base));
int quotients[digNum];
quotients[0]=num;
for(int i=1;i<digNum;++i){
quotients[i]=(int)floor(num/base);
num=(unsigned long int)floor(num/base);
}
dest[0]=quotients[digNum-1];
for(int i=1;i<digNum;++i)
dest[i]=quotients[digNum-i-1]-quotients[digNum-i]*base;
return digNum;
}
#endif
```

```
#include <iostream>
#include <conio.h>
#include <stdlib.h>
#include "Base.h"
using namespace std;
int main(){
while(1){
system("cls");
int choice;
cout<<"\n\n\n 1-Convert from decimal";
cout<<"\n 2-Convert to decimal";
cout<<"\n 3-Exit";
cout<<"\n\n Enter your choice:";
cin>>choice;
switch(choice){
case 1:{
system("cls");
unsigned long int num;
int base,n;
cout<<"\n\n Enter the number:";
cin>>num;
cout<<" Enter the destination base:";
cin>>base;
string Num=DecToB(num,base,n);
cout<<"\n The number you entered is ";
for(int i=0;i<n;++i)
cout<<Num[i];
cout<<" in base "<<base;
cout<<"\n\n (Press any key to return to the main screen)";
getch();
continue;
}
case 2:{
system("cls");
string Num;
int base;
cout<<"\n\n Enter the number:";
cin>>Num;
cout<<" Enter its base:";
cin>>base;
cout<<"\n The nuber you entered is "<<BToDec(Num,base)<<" in decimal";
cout<<"\n\n (Press any key to return to the main screen)";
getch();
continue;
}
case 3:
return 0;
}
}
return 0;
}
```