Monday, January 9, 2012

Converting text to Binary

#include<stdio.h>
#include<string.h>
void main()
{
int d,i=0,j=0,bin[8]={0};
char list[20],c;
clrscr();
printf("enter text press enter at last-");


while((c=getchar())!='\n')        //storing text into array using getchar()
list[i++]=c;


i=0;


while(i<strlen(list))                    //logic starts here
{
d=list[i];                                              // storing character in an dummy var.
j=0;
while(d>=1)                                      // binary conversion logic starts from here
{
if(d==1)
{
bin[j]=1;
break;
}
bin[j]=d%2;
d=d/2;
j++;
}                         


for(j=7;j>=0;j--)                          //this loop is for printing the binary form
printf("%d",bin[j]);
printf("\n");
i++;
}


getch();
}


No comments:

Post a Comment