title: Base64 decoding - bit shift results in unexpected end of char array in C
body: <p>I already used a lot of different functions to decode Base64 encoded char array in C. Right now, I am using this function to decode Base64 char array.</p>
  <pre><code>    unsigned int b64_decode(const unsigned char* in, unsigned int in_len, unsigned char* out) {
  
      unsigned int i=0, j=0, k=0, s[4];
  
      for (i=0;i&lt;in_len;i++) {
          s[j++]=b64_int(*(in+i));
          if (j==4) {
              out[k+0] = ((s[0]&amp;255)&lt;&lt;2)+((s[1]&amp;0x30)&gt;&gt;4);
              if (s[2]!=64) {
                  out[k+1] = ((s[1]&amp;0x0F)&lt;&lt;4)+((s[2]&amp;0x3C)&gt;&gt;2);
                  if ((s[3]!=64)) {
                      out[k+2] = ((s[2]&amp;0x03)&lt;&lt;6)+(s[3]); k+=3;
                  } else {
                      k+=2;
                  }
              } else {
                  k+=1;
              }
              j=0;
          }
      }
  
      return k;
  }
  </code></pre>
  <p>The problem is, that for a Base64 string like this <b>QKSRjAEAAAAB3bW3rVpoJOA8bsb3eEuEEDiq</b>, I am getting a string with only five characters, even the decoded text should be much longer. I tried to debug the code and it is because the result of the bit shift for the sixth character is 0, which unexpectedly ends the string. Online Base64 decoders decoded the mentioned string without problems.</p>
  <p>I then want to convert the decoded string to hex. I am doing that like this:</p>
  <pre><code>for (int i = 0, j = 0; i &lt; size; ++i, j += 2) {
                  sprintf(data_hex + j, &quot;%02x&quot;, decoded_data[i] &amp; 0xff);
              }
  </code></pre>
  <p>Example of expected input: <strong>QKSRjAEAAAAB3bW3rVpoJOA8bsb3eEuEEDiq</strong></p>
  <p>Example of expected output: <strong>40a4918c0100000001ddb5b7ad5a6824e03c6ec6f7784b841038aa</strong></p>
  <p>Thank you for your response.</p>