Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I also had trouble finding working code for the iPhone that I could understand.</p> <p>I finally wrote this.</p> <pre><code>-(NSString *)Base64Encode:(NSData *)data; -(NSString *)Base64Encode:(NSData *)data{ //Point to start of the data and set buffer sizes int inLength = [data length]; int outLength = ((((inLength * 4)/3)/4)*4) + (((inLength * 4)/3)%4 ? 4 : 0); const char *inputBuffer = [data bytes]; char *outputBuffer = malloc(outLength+1); outputBuffer[outLength] = 0; //64 digit code static char Encode[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; //Start the count int cycle = 0; int inpos = 0; int outpos = 0; char temp; //Pad the last to bytes, the outbuffer must always be a multiple of 4. outputBuffer[outLength-1] = '='; outputBuffer[outLength-2] = '='; /* http://en.wikipedia.org/wiki/Base64 Text content M a n ASCII 77 97 110 8 Bit pattern 01001101 01100001 01101110 6 Bit pattern 010011 010110 000101 101110 Index 19 22 5 46 Base64-encoded T W F u */ while (inpos &lt; inLength){ switch (cycle) { case 0: outputBuffer[outpos++] = Encode[(inputBuffer[inpos] &amp; 0xFC) &gt;&gt; 2]; cycle = 1; break; case 1: temp = (inputBuffer[inpos++] &amp; 0x03) &lt;&lt; 4; outputBuffer[outpos] = Encode[temp]; cycle = 2; break; case 2: outputBuffer[outpos++] = Encode[temp|(inputBuffer[inpos]&amp;0xF0) &gt;&gt; 4]; temp = (inputBuffer[inpos++] &amp; 0x0F) &lt;&lt; 2; outputBuffer[outpos] = Encode[temp]; cycle = 3; break; case 3: outputBuffer[outpos++] = Encode[temp|(inputBuffer[inpos]&amp;0xC0) &gt;&gt; 6]; cycle = 4; break; case 4: outputBuffer[outpos++] = Encode[inputBuffer[inpos++] &amp; 0x3f]; cycle = 0; break; default: cycle = 0; break; } } NSString *pictemp = [NSString stringWithUTF8String:outputBuffer]; free(outputBuffer); return pictemp; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload