HEXADECIMAL to OCTAL number system conversion in C
Following program converts a hexadecimal number scanned from user into octal number system and prints the same. The only constraint is this program only converts hexadecimal numbers without any floating points.
Download C program file to run on your device at the end of this post.
C Program:
Download C program file to run on your device at the end of this post.
C Program:
#include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> #include<alloc.h> char* hextobin(char*, int); char* bintooct(char*,int); void convert1(char*, char*); void convert2(char*, char*); void convert3(char*, char*); void processing(int,int,char*,char*); void main() { char hex[100],oct[100],binary[100]; int n,n2; char*ptr; char*ptr2; clrscr(); printf("\n Enter HEXADECIMAL NUMBER without any floating point :"); scanf("%s",hex); n= strlen(hex); printf("\n length is %d",n); ptr=binary; ptr=hextobin(hex,n); n2= strlen(ptr); printf("\n hexadecimal %s is %s in binary.\n",hex,ptr); ptr2=oct; ptr2= bintooct(ptr,n2); printf("\n Binary %s is %s in octal. \n",ptr,ptr2); printf( " for more programs visit www.EngineersBurger.com\n -Jay Akbari "); getch(); } //to convert hexadecimal to binary. char* hextobin(char* hex, int n) { int i,j,k,count=0; char* bin; char temp[5]; bin= (char*) malloc(n*4*sizeof(char)+1); for(i=0; hex[i]!='\0';i++) { switch(hex[i]) { case '0': strcpy(temp,"0000"); break; case '1': strcpy(temp,"0001"); break; case '2': strcpy(temp,"0010"); break; case '3': strcpy(temp,"0011"); break; case '4':strcpy(temp,"0100"); break; case '5':strcpy(temp,"0101"); break; case '6':strcpy(temp,"0110"); break; case '7':strcpy(temp,"0111"); break; case '8':strcpy(temp,"1000"); break; case'9' :strcpy(temp,"1001"); break; case'a' :strcpy(temp,"1010"); break; case 'b' : strcpy(temp,"1011"); break; case 'c': strcpy(temp,"1100"); break; case 'd': strcpy(temp,"1101"); break; case 'e': strcpy(temp,"1110"); break; case 'f': strcpy(temp,"1111"); break; } k=count; for(j=0; temp[j]!='\0';j++) { *(bin+k)=temp[j]; k++; } count=k; } *(bin+k)='\0'; return (bin); } //to convert binary to octal char* bintooct(char*ptr,int n) { char*temp; if( (n%3)==0) { temp=(char*)malloc( n/3*sizeof(char)+1 ); convert1(ptr,temp); return (temp); } else if ( (n%3)==1 ) { temp=(char*) malloc(n/3*sizeof(char)+2 ); convert2(ptr,temp); return (temp); } else //n%3==2 { temp=(char*) malloc(n/3*sizeof(char)+2); convert3(ptr,temp); return (temp); } } void convert1(char*ptr,char*temp) { int i,j; for(i=0,j=0; *(ptr+i)!='\0';i+=3,j++) { processing(i,j,ptr,temp); } *(temp+j)='\0'; } void convert2(char* ptr,char* temp) { int i,j; //checking left most binary is 1 or zero. if( *(ptr)=='0') //eg:0111 *(temp)='0'; else *(temp)='1'; //eg:1111 for(i=1,j=1; *(ptr+i)!= '\0';i+=3,j++) { processing(i,j,ptr,temp); } *(temp+j)='\0'; } void convert3(char*ptr, char* temp) { int i,j; //checkingthe first two on left most binary is 1 or zero. if( *(ptr)=='0'&& *(ptr+1)=='0') //eg:00 111 111 *(temp)='0'; else if (*(ptr)=='0' && *(ptr+1)=='1') *(temp)='1'; //eg:01 111 111 else if( *ptr=='1' && *(ptr+1)=='0' ) *(temp) ='2'; else //star of ptr and ptr+1 both are 1 *(temp)='3'; for(i=2,j=1; *(ptr+i)!= '\0';i+=3,j++) { processing(i,j,ptr,temp); } *(temp+j)='\0'; } //this function is block of code which is common to above 3 functions. void processing(int i,int j,char* ptr,char* temp) { if ( *(ptr+i)=='0' && *(ptr+i+1)=='0' && *(ptr+i+2)=='0') *(temp+j)='0'; else if ( *(ptr+i)=='0' && *(ptr+i+1)=='0' && *(ptr+i+2)=='1') *(temp+j)='1'; else if ( *(ptr+i)=='0' && *(ptr+i+1)=='1' && *(ptr+i+2)=='0') *(temp+j)='2'; else if ( *(ptr+i)=='0' && *(ptr+i+1)=='1' && *(ptr+i+2)=='1') *(temp+j)='3'; else if ( *(ptr+i)=='1' && *(ptr+i+1)=='0' && *(ptr+i+2)=='0') *(temp+j)='4'; else if ( *(ptr+i)=='1' && *(ptr+i+1)=='0' && *(ptr+i+2)=='1') *(temp+j)='5'; else if ( *(ptr+i)=='1' && *(ptr+i+1)=='1' && *(ptr+i+2)=='0') *(temp+j)='6'; else // ( *(ptr+i)=='1' && *(ptr+i+1)=='1' && *(ptr+i+2)=='1') *(temp+j)='7'; }
Download C program file: CLICK HERE
0 comments:
Post a Comment