Can anyone edit this program from "Base 10 to any base" --->>> to "From any base to base 10 with character array" .I would really appreciate this a lot. I use turbo c++ compiler.

#include <iostream.h>
#include <conio.h>
#include <stdio.h>

void main(){

    char value;
    char t, u, v;
    char answer;

    do{

    clrscr();

    cout<<"**********NUMBER CONVERSION**********"<<endl;
    cout<<endl;

    cout<<"a - Binary"<<endl;
    cout<<"b - Octal"<<endl;
    cout<<"c - Decimal"<<endl;
    cout<<"d - Hexadecimal"<<endl;
    cout<<endl;

    cout<<"Select a value to be converted: ";
    cin>>value;

    switch(value){

    case 'a':
        cout<<endl<<"[BINARY CONVERSION]"<<endl;
        cout<<endl;

        cout<<"a - Octal"<<endl;
        cout<<"b - Decimal"<<endl;
        cout<<"c - Hexadecimal"<<endl;
        cout<<endl;

        cout<<"Convert to: ";
        cin>>t;

        switch(t){

            case 'a':                           //ok
                cout<<endl<<"[BINARY TO OCTAL CONVERSION]"<<endl;
                cout<<endl;

                long b1,f1=1,d1=0,num5,result5[8]={0,0,0,0,0,0,0,0},flag5=0;
                cout<<"Enter a Binary number: ";
                cin>>b1;

                cout<<"\n\n";

                while(b1>0)
                {
                   if((b1%10)==1)
                   {
                      d1=d1+f1;
                   }
                   b1=b1/10;
                   f1=f1*2;
                }

                num5=d1;
                cout<<"Octal equivalent is: ";
                for(int i5=0;i5<8;++i5)
                {
                   result5[7-i5]=num5%8;
                   num5=num5/8;

                   if(num5==0)
                      break;
                }

                for(i5=0;i5<8;++i5)
                {
                   if(result5[i5]!=0)
                      flag5=1;

                   if(flag5==1)
                      cout<<result5[i5];
                }

                cout<<endl;
                break;

            case 'b':                           //ok
                cout<<endl<<"[BINARY TO DECIMAL CONVERSION]"<<endl;
                cout<<endl;

                long b2,f2=1,d2=0;
                cout<<"Enter a Binary number: ";
                cin>>b2;

                cout<<"\n\n";

                while(b2>0)
                {
                   if((b2%10)==1)
                   {
                      d2=d2+f2;
                   }
                   b2=b2/10;
                   f2=f2*2;
                }

                cout<<"Decimal equivalent is: "<<d2<<endl;
                break;

            case 'c':                           //ok
                cout<<"[BINARY TO HEXADECIMAL CONVERSION]"<<endl;
                cout<<endl;

                long b3,f3=1,d3=0,r6[10],z6=0,number6;
                cout<<"Enter a Binary number: ";
                cin>>b3;

                cout<<"\n\n";

                while(b3>0)
                {
                   if((b3%10)==1)
                   {
                      d3=d3+f3;
                   }
                   b3=b3/10;
                   f3=f3*2;
                }

                number6=d3;
                while(number6>0)
                {
                   r6[z6]=number6%16;
                   number6=number6/16;
                   z6++;
                }

                cout<<"Hexadecimal equivalent is: ";

                for(int j6=z6-1;j6>=0;j6--)
                {
                   if(r6[j6]==10)
                      cout<<"A";
                   else if(r6[j6]==11)
                      cout<<"B";
                   else if(r6[j6]==12)
                      cout<<"C";
                   else if(r6[j6]==13)
                      cout<<"D";
                   else if(r6[j6]==14)
                      cout<<"E";
                   else if(r6[j6]==15)
                      cout<<"F";
                   else
                      cout<<r6[j6];
                }

                cout<<endl;
                break;

            default:
                cout<<endl;
        }

        break;

    case 'b':
        cout<<endl<<"[OCTAL CONVERSION]"<<endl;
        cout<<endl;

        cout<<"a - Binary"<<endl;
        cout<<"b - Decimal"<<endl;
        cout<<"c - Hexadecimal"<<endl;
        cout<<endl;

        cout<<"Convert to: ";
        cin>>u;

        switch(u){

            case 'a':                           //ok
                cout<<endl<<"[OCTAL TO BINARY CONVERSION]"<<endl;
                cout<<endl;

                long number4,dec7,rem7,i7=1,sum7=0;
                cout<<"Enter an Octal number: ";
                scanf("%o", &number4);

                cout<<"\n\n";

                dec7=printf("%d", number4);

                while(dec7>0)
                {
                   rem7=dec7%2;
                   sum7=sum7 + (i7*rem7);
                   dec7=dec7/2;
                   i7=i7*10;
                }

                cout<<"Binary equivalent is: "<<sum7<<endl;
                break;

            case 'b':                           //ok
                cout<<endl<<"[OCTAL TO DECIMAL CONVERSION]"<<endl;
                cout<<endl;

                long number5;
                cout<<"Enter an Octal number: ";
                scanf("%o", &number5);

                cout<<"\n\n";

                printf("Decimal equivalent is: %d", number5);
                break;

            case 'c':                           //ok
                cout<<endl<<"[OCTAL TO HEXADECIMAL CONVERSION]"<<endl;
                cout<<endl;

                long number6;
                cout<<"Enter an Octal number: ";
                scanf("%o", &number6);

                cout<<"\n\n";

                printf("Hexadecimal equivalent is: %X", number6);
                break;

            default:
                cout<<endl;
        }

        break;

    case 'c':
        cout<<endl<<"[DECIMAL CONVERSION]"<<endl;
        cout<<endl;

        cout<<"a - Binary"<<endl;
        cout<<"b - Octal"<<endl;
        cout<<"c - Hexadecimal"<<endl;
        cout<<endl;

        cout<<"Convert to: ";
        cin>>v;

        switch(v){

            case 'a':                           //ok
                cout<<endl<<"[DECIMAL TO BINARY CONVERSION]"<<endl;
                cout<<endl;

                long dec1,rem1,i1=1,sum1=0;
                cout<<"Enter a Decimal number: ";
                cin>>dec1;

                cout<<"\n\n";

                while(dec1>0)
                {
                   rem1=dec1%2;
                   sum1=sum1 + (i1*rem1);
                   dec1=dec1/2;
                   i1=i1*10;
                }

                cout<<"Binary equivalent is: "<<sum1<<endl;
                break;

            case 'b':                           //ok
                cout<<endl<<"[DECIMAL TO OCTAL CONVERSION]"<<endl;
                cout<<endl;

                long num2,result[8]={0,0,0,0,0,0,0,0},flag2=0;
                cout<<"Enter a Decimal number: ";
                cin>>num2;

                cout<<"\n\n";

                cout<<"Octal equivalent is: ";
                for(int i2=0;i2<8;++i2)
                {
                   result[7-i2]=num2%8;
                   num2=num2/8;

                   if(num2==0)
                      break;
                }

                for(i2=0;i2<8;++i2)
                {
                   if(result[i2]!=0)
                      flag2=1;

                   if(flag2==1)
                      cout<<result[i2];
                }

                cout<<endl;
                break;

            case 'c':                           //ok
                cout<<endl<<"[DECIMAL TO HEXADECIMAL CONVERSION]"<<endl;
                cout<<endl;

                long r[10],z=0,number;
                cout<<"Enter a Decimal number: ";
                cin>>number;

                cout<<"\n\n";

                while(number>0)
                {
                   r[z]=number%16;
                   number=number/16;
                   z++;
                }

                cout<<"Hexadecimal equivalent is: ";

                for(int j=z-1;j>=0;j--)
                {
                   if(r[j]==10)
                      cout<<"A";
                   else if(r[j]==11)
                      cout<<"B";
                   else if(r[j]==12)
                      cout<<"C";
                   else if(r[j]==13)
                      cout<<"D";
                   else if(r[j]==14)
                      cout<<"E";
                   else if(r[j]==15)
                      cout<<"F";
                   else
                      cout<<r[j];
                }

                cout<<endl;
                break;

            default:
                cout<<endl;
        }

        break;

    case 'd':
        cout<<endl<<"[HEXADECIMAL CONVERSION]"<<endl;
        cout<<endl;

        cout<<"a - Binary"<<endl;
        cout<<"b - Octal"<<endl;
        cout<<"c - Decimal"<<endl;
        cout<<endl;

        cout<<"Convert to: ";
        cin>>v;

        switch(v){

            case 'a':                           //ok
                cout<<endl<<"[HEXADECIMAL TO BINARY CONVERSION]"<<endl;
                cout<<endl;

                long number1,dec8,rem8,i8=1,sum8=0;
                cout<<"Enter an Hexadecimal number: ";
                scanf("%X", &number1);

                cout<<"\n\n";

                dec8=printf("%d", number1);

                while(dec8>0)
                {
                   rem8=dec8%2;
                   sum8=sum8 + (i8*rem8);
                   dec8=dec8/2;
                   i8=i8*10;
                }

                cout<<"Binary equivalent is: "<<sum8<<endl;
                break;

            case 'b':                           //ok
                cout<<endl<<"[HEXADECIMAL TO OCTAL CONVERSION]"<<endl;
                cout<<endl;

                long number2;
                cout<<"Enter a Hexadecimal number: ";
                scanf("%X", &number2);

                cout<<"\n\n";

                printf("Octal equivalent is: %o", number2);
                break;

            case 'c':                           //ok
                cout<<endl<<"[HEXADECIMAL TO DECIMAL CONVERSION]"<<endl;
                cout<<endl;

                long number3;
                cout<<"Enter a Hexadecimal number: ";
                scanf("%X", &number3);

                cout<<"\n\n";

                printf("Decimal equivalent is: %d", number3);
                break;

            default:
                cout<<endl;
        }

        break;

    default:
        cout<<endl;
    }
    cout<<endl;

    cout<<"Do you want to continue NUMBER CONVERSION?(y/n) ";
    cin>>answer;
    }

    while(answer == 'y');
    cout<<endl;
}

Recommended Answers

All 8 Replies

Just edit this to any base to base 10 with character array.

Im really desperate of help right now :(

Show some better effort rather than copy pasting.. I really shouldn't be giving you code for apparently no reason but I feel nicer for some reason..

It can be done as follows:

#include <iostream>
#include <sstream>

int tobase10(const char* Value)
{
    int Dec = 0, Bin = strtoul(Value, 0, 10);

    for (int I = 0; Bin > 0; ++I)
    {
        if(Bin % 10 == 1)
        {
            Dec += (1 << I);
        }
        Bin /= 10;
    }

    return Dec;
}

int tobase10(int num)
{
    int res = 0;
    std::stringstream ss;
    ss << std::dec << num;
    ss >> res;
    return res;
}

int main()
{
    std::cout<<tobase10("11111111")<<"\n"; //binary to dec.
    std::cout<<tobase10(0377)<<"\n";       //octal to dec.
    std::cout<<tobase10(0xFF)<<"\n";       //hex to dec.
    return 0;
}

Dude thank you very much. But what compiler you use for this source codes? Because Im using turbo c++ and the source code you present to me aren't working to my turbo c++ compiler. I don't know any up-to-date compilers.

Oops ... see below ...

Sorry I only use Turbo C++. BUt thanks anyway. Can anyone reverse this code in "TURBO C++" from base 10 to any base ----> to any base to base 10.

Decimal to Binary, Octal and HEX converter

#include<stdio.h>
#include<conio.h>
#define MAX 79
#define ESC 27
void main()
{
         char res,r;
         int i,k=0,y;
         long j=0,l=0,o=0;
         textcolor(15);
         for(i=0;i<5000;i++)
                 cprintf(" ");
         while(1)
         {
                 clrscr();
                 printf("Enter Any Number To Be Converted : ");
                 scanf("%ld",&j);
                 l=j;
                 o=l;
                 k=0;
                 if(j>2145555550)
                 {
                         textcolor(4);
                         gotoxy(15,5);
                         cprintf("The Number Is Greater Than Range");
                         textcolor(15);
                         getch();
                         goto end;
                 }
                 else
                 {
                         gotoxy(1,6);
                         printf("Binary Equivalent:-");
                         y=MAX;
                         while(j>0)
                         {
                                 k=j%2;
                                 j=j/2;
                                 gotoxy(y,6);
                                 y--;
                                 printf("%d",k);
                         }
                         gotoxy(1,8);
                         printf("Hexadecimal Equivalent:- ");
                         y=MAX;
                         while(l>0)
                         {
                                 k=l%16;
                                 l=l/16;
                                 gotoxy(y,8);
                                 y--;
                                 if(k<=9)
                                 {
                                         printf("%d",k);
                                 }
                                 if(k==10)
                                 {
                                         printf("A");
                                 }
                                 if(k==11)
                                 {
                                         printf("B");
                                 }
                                 if(k==12)
                                 {
                                         printf("C");
                                 }
                                 if(k==13)
                                 {
                                         printf("D");
                                 }
                                 if(k==14)
                                 {
                                         printf("E");
                                 }
                                 if(k==15)
                                 {
                                         printf("F");
                                 }
                         }
                         gotoxy(1,10);
                         printf("Octal Equivalent:- ");
                         y=MAX;
                         while(o>0)
                         {
                                 k=o%8;
                                 o=o/8;
                                 gotoxy(y,10);
                                 y--;
                                 printf("%d",k);
                         }
                 }
                 gotoxy(15,25);
                 printf("Press Any Key To Continue , ESC To Exit");
                 res=getch();
                 if(res==ESC)
                 {
                         break;
                 }
         }
         end:
         textcolor(7);
         for(i=0;i<5000;i++)
                 cprintf(" ");
         clrscr();
}

You are way over-due to UPDATE to a modern C++ compiler ...

So many are free ... and UP-TO-DATE !!!

Or ...

since you are really using a lot of C code ...

you could recode it all in C, and repost fresh in the C forum here ...

(and, do not use conio.h, clrscr, gotoxy, etc... to keep your code portable),

with what errors you are getting ... where (what function ... what line) you think you are having your first coding problem?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.