title: C programming base64 encoding special characters
body: <p>I need to create a function that encodes file to base64 and returns the count of chars read.
  So the problem is that my current code runs into a problem when it encounters a special character like ÿ, so what would be the easiest way to fix it?</p>
  
  <pre><code>static void encodeblock( unsigned char *in, unsigned char *out, int len )
  {
      out[0] = encoding[ (int)(in[0] &gt;&gt; 2) ];
      out[1] = encoding[ (int)(((in[0] &amp; 0x03) &lt;&lt; 4) | ((in[1] &amp; 0xf0) &gt;&gt; 4)) ];
      out[2] = (len &gt; 1 ? encoding[ (int)(((in[1] &amp; 0x0f) &lt;&lt; 2) | ((in[2] &amp; 0xc0) &gt;&gt; 6)) ] : '=');
      out[3] = (len &gt; 2 ? encoding[ (int)(in[2] &amp; 0x3f) ] : '=');
  }
  
  int to_base64(const char *dst_file, const char *src_file)
  {
      unsigned char in[3];
          unsigned char out[4];
      int i, len, blocksout = 0;
      int count = 0;
  
      FILE *infile;
      infile = fopen(src_file,"r");
      if(infile==NULL){
          return -1;
      }
      FILE *outfile;
      outfile = fopen(dst_file,"w");
      if(outfile==NULL){
          return -1;
      }
  
          *in =  0;
          *out = 0;
      while( feof( infile ) == 0 ) {
          len = 0;
          for( i = 0; i &lt; 3; i++ ) {
              in[i] = fgetc(infile);
  
  
              if(feof(infile) == 0 ) {
                  len++;
                  count+=1;
              }
              else {
                  in[i] = 0;
              }
          }
          if( len &gt; 0 ) {
              encodeblock( in, out, len );
              for( i = 0; i &lt; 4; i++ ) {
                  putc( out[i], outfile );
  
  
              }
              blocksout++;
          }
          if( blocksout &gt;= (64/4) || feof( infile ) != 0 ) {
              if( blocksout &gt; 0 ) {
                  fprintf( outfile, "\n" );
              }
              blocksout = 0;
          }
      }
          fclose(infile);
          fclose(outfile);
      return( count );
  }
  </code></pre>