How to convert NSData
to base64
. I have NSData
and want to convert into base64
how can I do this?
-
This is very close to this question: http://stackoverflow.com/questions/392464/any-base64-library-on-iphone-sdk – Brad Larson Feb 16 '10 at 13:21
-
so, why do we need to convert NSData to base 64 in general? – zumzum Jun 19 '14 at 21:41
7 Answers
EDIT
As of OS X 10.9 / iOS 7, this is built into the frameworks.
See -[NSData base64EncodedDataWithOptions:]
Prior to iOS7/OS X 10.9:
Matt Gallagher wrote an article on this very topic. At the bottom he gives a link to his embeddable code for iPhone.
On the mac you can use the OpenSSL library, on the iPhone he writes his own impl.

- 12,933
- 4
- 29
- 32
-
2There seems to be a small issue in Matts code: In `void *NewBase64Decode` line 128, `j += accumulateIndex - 1;` has to be surrounded with `if (accumulatedIndex > 0) {...}`. Otherwise, if the encoded stream contains invalid (e.g. `\r\n`) chars at the end, the last character of the decoded text may get lost. This caused strange issues in my code doing line based decoding of IMAP responses... – DEAD10CC Dec 14 '12 at 13:06
-
It turns out there has been a function to do this since iOS4, but it was never released publicly until iOS 7. If imageData is an instance of NSData, then simply... [imageData base64Encoding]; https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/classes/NSData_Class/DeprecationAppendix/AppendixADeprecatedAPI.html – Charlie Martin May 21 '14 at 05:06
//from: http://cocoadev.com/BaseSixtyFour
+ (NSString*)base64forData:(NSData*)theData {
const uint8_t* input = (const uint8_t*)[theData bytes];
NSInteger length = [theData length];
static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
uint8_t* output = (uint8_t*)data.mutableBytes;
NSInteger i;
for (i=0; i < length; i += 3) {
NSInteger value = 0;
NSInteger j;
for (j = i; j < (i + 3); j++) {
value <<= 8;
if (j < length) {
value |= (0xFF & input[j]);
}
}
NSInteger theIndex = (i / 3) * 4;
output[theIndex + 0] = table[(value >> 18) & 0x3F];
output[theIndex + 1] = table[(value >> 12) & 0x3F];
output[theIndex + 2] = (i + 1) < length ? table[(value >> 6) & 0x3F] : '=';
output[theIndex + 3] = (i + 2) < length ? table[(value >> 0) & 0x3F] : '=';
}
return [[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] autorelease];
}
As an update, the iOS7 SDK has a category on NSData (NSDataBase64Encoding)
with methods
-[NSData base64EncodedStringWithOptions:]
-[NSData initWithBase64EncodedString:options:]
-[NSData initWithBase64EncodedData:options:]
-[NSData base64EncodedDataWithOptions:]
Should avoid having to roll your own category method

- 1,222
- 1
- 15
- 27
-
Wow, great to hear! Every time when it comes to base64, I'm wondering why it is not present in standard libs of so many languages... – DEAD10CC Sep 24 '13 at 19:04
-
1
-
Sorry, there are functionally equivalent methods available back to iOS 4(initWithBase64Encoding and base64Encoding) – Crake Sep 30 '13 at 20:13
-
-
One example is sending binary data to a REST web service interface. E.g. Say I have a photo photo property on a Person object, to send it via JSON, I can convert the `-[Person photo]` NSData property to an NSString by `-[[Person photo] base64EncodedStringWithOptions:0]` and so can be serialised by `NSJSONSerialization` – Damien Pontifex Oct 26 '13 at 11:11
Super easy drop-in Google library code here.
Just use +rfc4648Base64StringEncoding
to get an instance, then use the encode
/decode
functions.
It's a beautiful thing. (Don't forget to grab the header file and the GTMDefines.h
header from the root, though.)

- 13,251
- 7
- 69
- 80
Its not easy. As in there's no built in support for this in c or obj-c. Here's what Im doing (Which is basically having the CL do it for me):
- (NSString *)_base64Encoding:(NSString *) str
{
NSTask *task = [[[NSTask alloc] init] autorelease];
NSPipe *inPipe = [NSPipe pipe], *outPipe = [NSPipe pipe];
NSFileHandle *inHandle = [inPipe fileHandleForWriting], *outHandle = [outPipe fileHandleForReading];
NSData *outData = nil;
[task setLaunchPath:@"/usr/bin/openssl"];
[task setArguments:[NSArray arrayWithObjects:@"base64", @"-e", nil]];
[task setStandardInput:inPipe];
[task setStandardOutput:outPipe];
[task setStandardError:outPipe];
[task launch];
[inHandle writeData:[str dataUsingEncoding: NSASCIIStringEncoding]];
[inHandle closeFile];
[task waitUntilExit];
outData = [outHandle readDataToEndOfFile];
if (outData)
{
NSString *base64 = [[[NSString alloc] initWithData:outData encoding:NSUTF8StringEncoding] autorelease];
if (base64)
return base64;
}
return nil;
}
And you use it like this:
NSString *b64str = [strToConvert _base64Encoding:strToConvert];
And this isn't my code - I found it here: http://www.cocoadev.com/index.pl?BaseSixtyFour and it works great. You could always turn this into a +() method.
Oh, and to get your NSData to an NSString for this method:
NSString *str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

- 13,174
- 11
- 66
- 101
-
1On the mac it isn't necessary to spawn a process - you can directly use libcrypto. On the iPhone, is /usr/bin/openssl present? – Ken Feb 05 '10 at 15:28
iOS has always included built in support for base64 encoding and decoding. If you look at resolv.h
you should see the two functions b64_ntop
and b64_pton
. The Square SocketRocket library provides a reasonable example of how to use these functions from objective-c.
These functions are pretty well tested and reliable - unlike many of the implementations you may find in random internet postings.
Don't forget to link against libresolv.dylib
.
If you link against the iOS 7 SDK, you can use the newer methods initWithBase64Encoding:
and base64EncodedDataWithOptions:
. These exist in previous releases, but were private. So if you link against the 6 SDK, you may run into undefined behavior. This would be an example of how to use this only when linking against the 7 SDK:
#ifndef __IPHONE_7_0
// oh no! you are using something unsupported!
// Call and implementation that uses b64_pton here
#else
data = [[NSData alloc] initWithBase64Encoding:string];
#endif

- 21,123
- 4
- 76
- 83
I modified the code above to meet my needs, building an HTTP POST. I was able to skip the NSString step, and include line breaks in the BASE64 code, which at least one web server found more palatable:
#define LINE_SIZE 76
//originally from: http://www.cocoadev.com/index.pl?BaseSixtyFour
// via joshrl on stockoverflow
- (void) appendBase64Of: (NSData *)inData to:(NSMutableData *)outData {
const uint8_t* input = (const uint8_t*)[inData bytes];
NSInteger length = [inData length];
static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
uint8_t buf[LINE_SIZE + 4 + 2];
size_t n = 0;
NSInteger i;
for (i=0; i < length; i += 3) {
NSInteger value = 0;
NSInteger j;
for (j = i; j < (i + 3); j++) {
value <<= 8;
if (j < length) {
value |= (0xFF & input[j]);
}
}
buf[n + 0] = table[(value >> 18) & 0x3F];
buf[n + 1] = table[(value >> 12) & 0x3F];
buf[n + 2] = (i + 1) < length ? table[(value >> 6) & 0x3F] : '=';
buf[n + 3] = (i + 2) < length ? table[(value >> 0) & 0x3F] : '=';
n += 4;
if (n + 2 >= LINE_SIZE) {
buf[n++] = '\r';
buf[n++] = '\n';
[outData appendBytes:buf length:n];
n = 0;
}
}
if (n > 0) {
buf[n++] = '\r';
buf[n++] = '\n';
[outData appendBytes:buf length:n];
}
return;
}

- 634
- 7
- 12