title: Conversion of Image--->binary--->image using C
body: <p>We are trying to convert an image into binary data and vice-versa for a project using C programming. All the other solutions we found on the net are either in C++ or Java. Here is the approach we tried:</p>
  
  <ol>
  <li><p>Convert the image into a text file containing binary data. Each 8 characters corresponds to the character byte when the image is opened using a text editor.</p></li>
  <li><p>Then we try to reconvert the binary data into its respective characters using a C program. </p></li>
  <li><p>Then we open the result using Picasa Photoviewer. We get an invalid image. </p></li>
  </ol>
  
  <p>How do we get back the original image? Here is the code we used to convert the image into a text file:</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;conio.h&gt;
  
  void main()
  {
    clrscr();
    FILE *fptr;
    FILE *txt;
    int c;
  
    fptr=fopen("D:\\aa.bmp","r");
    txt=fopen("D:\\test1.txt","w");
  
    if(fptr==NULL)
    {
      printf("NOTHING In FILE");
      fclose(fptr);
    }
    else
    {
      printf("success");
  
      do
      {
        c=fgetc(fptr);
        for(int i=0;i&lt;=7;i++)
        {
          if(c&amp;(1&lt;&lt;(7-i)))
          {
            fputc('1',txt);
          }
          else
          {
            fputc('0',txt);
          }
        }
        // fprintf(txt,"\t");
      }while(c!=EOF);
  
    }
  
    fclose(fptr);
    fclose(txt);
  
    printf("writing over");
    getch();
  }
  </code></pre>
  
  <p>Here is the code to convert the resulting text file to image file full of binary characters, i.e. a text file with only ones and zeroes.</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;conio.h&gt;
  
  
  \\The following function converts the ones and zeroes in the text file into a character. 
  \\For example the text file may have the 8 consecutive characters '1','0','0','0','1','0','0','0'. 
  \\This converts it into the character equivalent of the binary \\value 10001000
  
  char bytefromtext(char* text) 
  {
    char result=0;
    for(int i=0;i&lt;8;i++)
    {
      if(text[i]=='1')
      {
        result |= (1&lt;&lt; (7-i) );
      }
    }
    return result;
  }
  
  void main()
  {
    clrscr();
    FILE *pfile;
    FILE *image;
    char buf[8];
    char c;
    int j=0;
  
    image=fopen("D:\\aa2.bmp","w"); //open an empty .bmp file to
                                    //write characters from the source image file
    pfile=fopen("D:\\test1.txt","r");
  
    if(pfile==NULL)
      printf("error");
    else
    {
      c=fgetc(pfile);
  
      while(c!=EOF)
      {
        buf[j++]=c;
        if(j==8)
        {
          fputc(bytefromtext(buf),image);
          j=0;
        }
        c=fgetc(pfile);
  
      }
  
      fclose(pfile);
      fclose(image);
    }
  
    getch();
  }
  </code></pre>
  
  <p>We get an invalid image when the characters are written into the .bmp file. When we open this new file using a text editor and also the image file using a text editor we get the same characters. </p>