Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are a few variants of this floating around that extend <code>NSString</code> or <code>NSData</code> through the use of Objective-C categories.</p> <p>Here's one example that I have added to my "utilities" toolbox:</p> <p>Header:</p> <pre><code>#import &lt;Foundation/NSString.h&gt; @interface NSString (Utilities) + (NSString *) base64StringFromData:(NSData *)data; @end </code></pre> <p>Implementation:</p> <pre><code>#import "NSString+Utilities.h" @implementation NSString (Utilities) + (NSString *) base64StringFromData:(NSData *)data { static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; if ([data length] == 0) return @""; char *characters = malloc((([data length] + 2) / 3) * 4); if (characters == NULL) return nil; NSUInteger length = 0; NSUInteger i = 0; while (i &lt; [data length]) { char buffer[3] = {0,0,0}; short bufferLength = 0; while (bufferLength &lt; 3 &amp;&amp; i &lt; [data length]) buffer[bufferLength++] = ((char *)[data bytes])[i++]; // Encode the bytes in the buffer to four characters, including padding "=" characters if necessary. characters[length++] = encodingTable[(buffer[0] &amp; 0xFC) &gt;&gt; 2]; characters[length++] = encodingTable[((buffer[0] &amp; 0x03) &lt;&lt; 4) | ((buffer[1] &amp; 0xF0) &gt;&gt; 4)]; if (bufferLength &gt; 1) characters[length++] = encodingTable[((buffer[1] &amp; 0x0F) &lt;&lt; 2) | ((buffer[2] &amp; 0xC0) &gt;&gt; 6)]; else characters[length++] = '='; if (bufferLength &gt; 2) characters[length++] = encodingTable[buffer[2] &amp; 0x3F]; else characters[length++] = '='; } return [[[NSString alloc] initWithBytesNoCopy:characters length:length encoding:NSUTF8StringEncoding freeWhenDone:YES] autorelease]; } @end </code></pre> <p>Example usage:</p> <pre><code>NSString *inputString = @"myInputString"; NSLog(@"%@", [NSString base64StringFromData:[inputString dataUsingEncoding:NSUTF8StringEncoding]]); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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