Conversions between number systems

Submitted by:Jhon Jhon

Category:C++

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.

Tags: convert number , conversion

Code Snippet:

``#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(){    int i_radix,o_radix;    double dx;    char cx;    char in[65] = {"\n"};    char out[65] = {"\n"};     char rad_name[16][14] = {"Unary","Binary","Ternary","Quaternary","Quinary","Senary",                            "Septal","Octal","Nonary","Decimal","Undecimal","Duodecimal",                            "triskadecimal","Tetradecimal","pentadecimal","Hexadecimal"};            in[0] = '\0';            out[0] = '\0';    cout << "To EXIT press Ctrl+z : " << endl;    cout << "Enter Radix for the input number system: ";    while (cin >> i_radix)    {            cout << "Enter Radix for the output number system: ";            cin >> o_radix;            cout << "Enter the number: ";            cin >> in;            if( i_radix > 10)              strcpy( in , low_upper(in));           dx = convx_d( in , i_radix);            if( in[0] == '-')               cx = '-';            else               cx = '0';           strcpy(out, convd_x(dx, o_radix, cx));                 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] = '\0';                out[0] = '\0';     }    return 0;}double convx_d(char* in , int r){    int i = 0 ,j;    long lx = 0;    double dx = 0.0;    if(in[0] == '-')        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[65] ={"\n"};    char out[65] = {"\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;}``