Upcoming SlideShare
×

# Bin to hex

637

Published on

computer program..
bin to hex convertion upto 4 bit.

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
637
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
0
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Bin to hex

1. 1. BINARY TO HEXADECIMAL CONVERSION COMPUTER-PROGRAMMING
2. 2. Main program:#include<stdio.h>#include<conio.h>#include<math.h>#include<dos.h>#include<stdlib.h>#include<string.h>//- thats the mouse area----union REGS in,out; int callmouse(void) { in.x.ax=1; int86(51,&in,&out); return (1); } void mouseposi(int* xpos,int* ypos,int* click) { in.x.ax=3; int86(51,&in,&out); (*click)=out.x.bx; //used for knowing the state of click . . (*xpos)=out.x.cx; //simply tells x position . . (*ypos)=out.x.dx; //simply tells y position . . } int mousehide() { in.x.ax=2; int86(51,&in,&out); return 1; } void setposi(int* xpos,int* ypos) { in.x.ax=4; in.x.cx=*xpos; in.x.dx=*ypos; int86(51,&in,&out); }//----end of mouse funtions---------//thats the function which checks wheather the given input is//ok mean is it binary or not if the input is binary the function//returns 1 else 1
3. 3. int check_input(char _input[]) { int loop,len = strlen(_input); //getting length of the input array for(loop = 0; loop < len ; loop++) //looping through . . . { if(_input[loop]!=1 && _input[loop]!=0) return 0; //if any one element is mis matching returning 0 } return 1; //if everything is fine and there are only 1s and 0s returning 1 }//end of the checking input funtion . . . .int bin(int);main(){startagain: int a[25],i,count=0,b[25],c[25],sum=0; //int l =0; int n = 0; int check = 0; int num = 0; int x = 100,y = 200; int xgot,ygot,click; char ch,mychar = 1; char input[15];//just printing the menu clrscr(); gotoxy(15,8); printf("Welcome to Program.n"); gotoxy(10,10); printf("Binary to Hexa-decimal Conversion.n"); gotoxy(10,11); printf("==================================n"); gotoxy(10,13); printf("Please select from the following Menu:n"); program: puts("nnntPRESS 1 TO DO ALL WITH KEYBOARD."); puts("ntPRESS 2 TO DO ALL WITH MOUSE."); mychar = getch(); if(mychar == 1) {//if choice is simple keyboad just do this .. . puts("ntt1. For Program "); puts("tt0. To exit"); ch = getch(); }
4. 4. else if(mychar== 2) { clrscr(); //call mouse and set some initial position . . . callmouse(); setposi(&x,&y); //write some text to guide the user . .. puts("nnt Click Here For Program. "); puts("nnt Click Here To Exit."); click = 0; while(1) //run infinite loop so through its help we can {//get mouseposi again and again and can check where the mouse is . .. //asking mouse current position . .. mouseposi(&xgot,&ygot,&click); //simply checking that if user has clicked and the mouse in the first row area //then set ch variable to 1 and break . .. if(ygot > 10 && ygot < 27 && click == 1) { ch = 1; break; }//if the click has been done on 2nd row area then user wants to exit//so set the ch = 0 so the upcoming switch statement can know what user//wants to do .. . if(ygot >29 && ygot <42 && click== 1) { ch = 0; break; } } //end of while } // end of if else { printf("choose again you pressed wrong key. . . nn"); goto startagain; } switch(ch){ case 1: reask: //simply taking input ... clrscr();
5. 5. printf("nnttEnter the number in a Binary form: nnttt"); gets(input);//cheching is the input is binary or not with our hand made funtion check_input check = check_input(input); if(check == 0) {//if number entered was not binary give eror and start program again printf("nnt ERROR PLEASE ENTER BINARY ONLY : "); getch(); main(); } //if its binary convert it into form of intiger n = atoi(input);//write that input in the file FILE* myfile = fopen("binaries.txt","a+"); fprintf(myfile,"%dn",n); fclose(myfile);//apply logic for converting n in decimal for(i=0;n>=1;i++) { a[i]=n%10; n=n/10; count=count+1; } for(i=0;i<=count-1;i++) { b[i]=pow(2,i); } for(i=0;i<=count-1;i++) { c[i]=a[i]*b[i]; sum=sum+c[i]; } //print decimal equavalent printf("ntt Decimal form= %d ttn",sum); num=bin(sum); break; case 0: { puts("nnnnt Thank you for using our program");
6. 6. puts("nnnnnnnntnnnnnnnttttttt Created By: HJ & SL."); break; default: puts("nttPlease select only 1 or 0"); goto program; } break;} getche();} // end of the main . . . . .void line(void){ int j; for(j=0;j<=43;j++) printf(" "); printf("n");} int bin(int num){ int b,c=0,a[5],i=0,count; b=num;//appaly logic for converting deci into hex . . .while(b>15){ a[i]=b%16; b=b/16; i++; c++;} a[i]=b; //print it . .. . printf("ntt Its Hexadecimal equilvant isnnttt"); for(i=c;i>=0;--i) { if(a[i] == 10) {printf("A");} else if(a[i]==11) {printf("B");} else if(a[i]==12) {printf("C");} else if(a[i]==13) {printf("D");}
7. 7. else if(a[i]==14) {printf("E");} else if(a[i]==15) {printf("F");} else {printf("%d",a[i]);} } // end of the for loop .. . .}