Search:

Submit Article

# Program that converts hexadecimal value in decimal, octal and binary.

Posted By: Easy Tutor     Category: C Programming     Views: 29178

## Code for Program that converts hexadecimal value in decimal, octal and binary. in C Programming

```#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;
}

{//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--)
{
}
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(30,3,"Press Escape to Quit");
textbackground(CYAN);
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();
}

}
```
Share:

 Easy Tutor author of Program that converts hexadecimal value in decimal, octal and binary. is from United States. Easy Tutor says Hello Friends,I am Free Lance Tutor, who helped student in completing their homework. I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] comI have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada. I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.Thanks,Happy Programming :) View All Articles

• Comment should be atleast 30 Characters.