# Conversions between number systems source code

This snippet submitted by Ali Nawkhas Murad on 2012-05-02. It has been viewed 36131 times.
Rating of 5.8 with 197 votes

```//General purpose converter program for conversion between any two number systems.
//As long as there is more than one number system in use, conversion of
//numbers from one system to another is important.
//By this program you can convert number systems with radix more than 16 also.
#include <iostream>
#include <string.h>
using namespace std;
char* low_upper(char* in)
{
for( int i = 0 ; in[i] != '\0' ; i++)
if( in[i] >= 'a' && in[i] <= 'z' )
in[i] = in[i] - 32;
return in;
}
double convx_d( char* , int );
char* convd_x(double , int, char );

int main()
{
double dx;
char cx;
char in = {"\n"};
char out = {"\n"};

"Septal","Octal","Nonary","Decimal","Undecimal","Duodecimal",
in = '\0';
out = '\0';
cout << "To EXIT press Ctrl+z : " << endl;
cout << "Enter Radix for the input number system: ";
{
cout << "Enter Radix for the output number system: ";
cout << "Enter the number: ";
cin >> in;

strcpy( in , low_upper(in));

dx = convx_d( in , i_radix);
if( in == '-')
cx = '-';
else
cx = '0';

cout << "\nThe number (" << in << ") in " << rad_name[i_radix-1] << " number system";
cout << "\n = (" << out << ") in " << rad_name[o_radix-1] << " number system.";
cout << "\n\nTo EXIT press Ctrl+z : ";
cout << "\nEnter Radix number for the input number: ";
in = '\0';
out = '\0';
}
return 0;
}
double convx_d(char* in , int r)
{
int i = 0 ,j;
long lx = 0;
double dx = 0.0;
if(in == '-')
i++;
while(in[i] != '\0' && in[i] != '.')
{
lx = lx * r + ((in[i] >= '0' && in[i] <= '9')? in[i] - 48 : in[i] - 55);
i++;
}
if( in[i] == '.')
{
j = i;
while( in[i] != '\0')//count number of steps after decimal point
{
i++;
}
i--;
double ir = (double) r;
while( i > j)
{
dx =  (dx + ((in[i] >= '0' && in[i] <= '9')? in[i] - 48 : in[i] - 55)) * 1.0/ir;
i--;
}
}
return dx += lx;
}
char* convd_x( double dx , int r, char cx)
{
int i = 0,j ,temp;
double fx;
char ctemp ={"\n"};
char out = {"\n"};
long lx = static_cast<long>(dx);
dx -= lx;
//                   calculation of integer part.
//***************************************************************
while( lx > 0)
{
temp = lx % r;
ctemp[i] = (temp >= 0 && temp <= 9 )? temp + 48 : temp + 55;
lx /= r;
i++;
}
ctemp[i] = '\0';
i--;
j = 0;
if( cx == '-')
{
out[j] = '-';
j++;
}
for(  ;  i >=0 ; i--, j++ )
out[j] = ctemp[i];
out[j] = '\0';

//                   calculation of fraction part.
//***************************************************************
if( dx > 0.0)
{
out[j] = '.';
j++;
i = j;
while( dx > 0.0000001 && j < i+9)
{
fx = dx * r;
temp = static_cast<int>(fx);
ctemp[j] = ((temp >= 0 && temp <= 9 )? temp + 48 : temp + 55);
dx = fx - temp;
j++;
}
for(  ;  i <= j ; i++ )
out[i] = ctemp[i];
out[i] = '\0';
}
return out;
}
/*Program's output
*******************************************
To EXIT press Ctrl+z :
Enter Radix for the input number system: 16
Enter Radix for the output number system: 10
Enter the number: cd56.8

The number (CD56.8) in Hexadecimal number system
= (52566.5) in Decimal number system.

To EXIT press Ctrl+z :
Enter Radix number for the input number: 8
Enter Radix for the output number system: 16
Enter the number: -723.56

The number (-723.56) in Octal number system
= (-1D3.B8) in Hexadecimal number system.

To EXIT press Ctrl+z :
Enter Radix number for the input number: 16
Enter Radix for the output number system: 8
Enter the number: -1d3.b8

The number (-1D3.B8) in Hexadecimal number system
= (-723.56) in Octal number system.

To EXIT press Ctrl+z :
Enter Radix number for the input number: ^Z

Process returned 0 (0x0)   execution time : 92.999 s
Press any key to continue.

*/```

More C and C++ source code snippets