Search This Blog

Loading...

Write a program that converts Hexadecimal value in Decimal, Octal and Binary value.

Write a program that converts hexadecimal value in decimal, octal and binary value.


#include<dos.h>
#include<conio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<stdio.h>
#define HEX 0
#define DEC 1
#define OCT 2
#define BIN 3

int i=0,choice=0;

void initmouse()
{
    _AX=0;
    geninterrupt(0x33);
}

void showmouse()
{
    _AX=1;
    geninterrupt(0x33);
}

void hidemouse()
{
    _AX=2;
    geninterrupt(0x33);
}

void getmouse(int *button,int *x,int *y)
{
    _AX=3;
    geninterrupt(0x33);
    *button=_BX;
    *x=_CX;
    *y=_DX;
}

int getnum(char c)//gets the numeric equivalent of a alphanumeric digit
{
    int j;
    char alpha_set[36]="0123456789abcdefghijklmnopqrstuvwzyz";
    for(j=0;j<36;j++)
    {
        if(alpha_set[j]==c)
            break;
    }
    return j;
}

unsigned long convert_to_decimal(char *_num,int radix)
{//converts a number of different base to decimalint i,len;
    unsigned long dec=0;
    len=strlen(_num);
    len--;
    for(i=0;_num[i]!=NULL;i++,len--)
    {
        dec+=(getnum(_num[i])*pow(radix,len));
    }
    return dec;
}


void display(int x,int y,charstring[80])
{//displays a string at the given coordinates
    gotoxy(x,y);
    cprintf(string);
}

void make_screen_cyan()//The name says it
{
    textbackground(CYAN);
    clrscr();
}

void start_screen()//Draws the Starting screen of the program
{
    i=0;
    hidemouse();// Hides the mouse
    make_screen_cyan();
    textbackground(WHITE);
    textcolor(BLACK);
    display(20,1,"Radix Converter");
    display(30,3,"Press Escape to Quit");
    textbackground(CYAN);
    display(10,5,"Hexadecimal:- ");
    display(10,7,"Decimal    :- ");
    display(10,9,"Octal      :- ");
    display(10,11,"Binary     :- ");
    textbackground(WHITE);
    display(23,5,"                                                      ");
    display(23,7,"                                                      ");
    display(23,9,"                                                      ");
    display(23,11,"                                                      ");
    if(choice==HEX)
        gotoxy(24,5);
    elseif(choice==DEC)
        gotoxy(24,7);
    elseif(choice==OCT)
        gotoxy(24,9);
    elseif(choice==BIN)
        gotoxy(24,11);
    showmouse();
}

void main()
{
    char text[80]="\0",buffer[80];
    char ch,*charhex,*chardec,*charoct,*charbin;
    int button,mousex,mousey,x,y;
    unsigned long deci;

    initmouse();//Initiates the mouse
    start_screen();
    showmouse();//Shows the mousewhile(1)
    {
        if(kbhit())
        {
            ch=getch();
            if(ch==27)        //27 is ASCII equivalent of Escpape keybreak;
            if(ch=='\b'&&wherex()>=24)   //If Backspace is pressed & cursor must not go outside input area
            {
                cprintf("\b");//Move cursor one space backwards
                cprintf("%c",255);//Put there a Space
                cprintf("\b");//Again move cursor backwardsif(i!=0)
                    i--;            //pop the char out of the text array
                text[i]=NULL;//put the NULL character to end the text
            }
            elseif(wherex()>=24&&ch>='0'&&ch<='f')
            {
                cprintf("%c",ch);
                text[i]=ch;            //push the char to the text array
                text[i+1]=NULL;    //End the array with NULL character
                i++;
            }
            x=wherex(),y=wherey();//Stores the current coordinates of cursor/*Explanation:- First convert the text string to a decimal. After it isconverted to decimal using convert_to_decimal(), convert the decimal to the required base, eg hex,oct or binary using ltoa() in stdlib.h.*/switch(choice)
            {
                case HEX:
                    deci=convert_to_decimal(text,16);
                    gotoxy(24,7);
                    printf("%ld                             ",deci);
                    gotoxy(24,11);
                    printf("%s                              ",ltoa(deci,buffer,2));
                    gotoxy(24,9);
                    printf("%s                              ",ltoa(deci,buffer,8));
                    break;
                case DEC:
                    deci=atol(text);
                    gotoxy(24,5);
                    printf("%s                             ",ltoa(deci,buffer,16));
                    gotoxy(24,9);
                    printf("%s                             ",ltoa(deci,buffer,8));
                    gotoxy(24,11);
                    printf("%s                             ",ltoa(deci,buffer,2));
                    break;
                case OCT:
                    deci=convert_to_decimal(text,8);
                    gotoxy(24,7);
                    printf("%ld                             ",deci);
                    gotoxy(24,5);
                    printf("%s                             ",ltoa(deci,buffer,16));
                    gotoxy(24,11);
                    printf("%s                              ",ltoa(deci,buffer,2));
                    break;
                case BIN:
                    deci=convert_to_decimal(text,2);
                    gotoxy(24,5);
                    printf("%s                            ",ltoa(deci,buffer,16));
                    gotoxy(24,7);
                    printf("%ld                            ",deci);
                    gotoxy(24,9);
                    printf("%s                            ",ltoa(deci,buffer,8));
                    break;
            }
            gotoxy(x,y);
        }
        getmouse(&button,&mousex,&mousey);
        mousex++,mousey++;
//The following lines detect where you have clicked & what you selectedif(mousex/8>23&&mousex/8<50&&mousey/8==4&&button==1)
            choice=HEX,start_screen();
        elseif(mousex/8>23&&mousex/8<50&&mousey/8==6&&button==1)
            choice=DEC,start_screen();
        elseif(mousex/8>23&&mousex/8<50&&mousey/8==8&&button==1)
            choice=OCT,start_screen();
        elseif(mousex/8>23&&mousex/8<50&&mousey/8==10&&button==1)
            choice=BIN,start_screen();
    }
}

No comments:

Post a Comment