4

I'm trying to encode and decode base64 data. but while decoding the base64 data, it returns bunch of hex values, but i couldn't display or printout using NSlog to the original readable strings. The below code couldn't print anything, just empty.

Can anyone help ? thanks > >

NSString* msgEncoded = [[NSString alloc] initWithFormat:@"Q1NNKE1DTC9TTUEgUkNWL2FkbWluQHNldGVjcy5jb20gT1JHLyBUVkIvNDNkYzNlMzQwYWQ3Yzkp:"];  
NSData* decoded = [[NSData alloc] initWithData:[self decodeBase64WithString:msgEncoded]];
NSString* plainString = [[NSString alloc]initWithData:decoded encoding:NSUTF8StringEncoding];
NSLog(@"\n Decoded string:  %@ \n", plainString );
hab
  • 627
  • 2
  • 9
  • 20
  • What Base64 lib are you using? – 8vius Aug 06 '12 at 16:27
  • Have you looked at the section of this article that talks about the iPhone? http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html – James Black Aug 06 '12 at 16:37
  • 2
    Show the `decodeBase64WithString:` method. – Justin Paulson Aug 06 '12 at 16:40
  • In this question I posted an `NSString` -> `NSString` decode and encode method:http://stackoverflow.com/questions/11633577/iphone-finalizing-apples-vague-verificationcontroller-m/11635024#11635024 – Justin Paulson Aug 06 '12 at 16:43
  • @JustinPaulson thanks for your recommendation but the decoding is having an error while trying to print the decoding (which is reversing to the original string). NSString* sampleString = @"hello habesh"; NSString* encoded = @"aGVsbG8gaGFiZXNo"; NSLog(@"\n Encode string 'hello girmay' %@",[self encodeString:sampleString]); NSLog(@"\n Decoded original string %@",[self decodeString:encoded]); – hab Aug 07 '12 at 19:12
  • That would be because there is no space (" ") encoding for base64. – Justin Paulson Aug 07 '12 at 19:43
  • Can u pls elaborate more since am new to such areas ? i didn't get the problem yet. – hab Aug 07 '12 at 21:03
  • spaces are just dismissed in base64 encoding. There is no encoding for spaces, so they are just ignored. So if you encode something with a space in it, and then try and decode the same string, you will not get the same result as what you encoded. – Justin Paulson Aug 08 '12 at 18:54

3 Answers3

8

There is a built in function in NSData

[data base64Encoding];
[data base64EncodedStringWithOptions:NSDataBase64Encoding76CharacterLineLength];
Nagaraj
  • 802
  • 9
  • 11
  • 1
    /* These methods first appeared in NSData.h on OS X 10.9 and iOS 7.0. They are deprecated in the same releases in favor of the methods in the NSDataBase64Encoding category. However, these methods have existed for several releases, so they may be used for applications targeting releases prior to OS X 10.9 and iOS 7.0. */ - (id)initWithBase64Encoding:(NSString *)base64String NS_DEPRECATED(10_6, 10_9, 4_0, 7_0); - (NSString *)base64Encoding NS_DEPRECATED(10_6, 10_9, 4_0, 7_0); – Jerry Jan 18 '14 at 00:07
5

If you are still having issues, try out this library: https://github.com/l4u/NSData-Base64

use it like so:

#import "NSData+Base64.h"

NSData *someData  //load your data from a file, url or photo as needed
NSData *file = [NSData dataWithContentsOfFile:@"mytextfile.txt"];
NSData *photo = UIImageJPEGRepresentation(self.photo.image,1);

//encode it
NSString *base64string = [photo base64EncodedString];
NSString *base64file = [file base64EncodedString];

//decode it
NSData *back = [NSData dataFromBase64String:base64string];
CocoaEv
  • 2,984
  • 20
  • 21
1

Try Google's GTMStringEncoding class. You'll need GTMDefines.h too.

GTMStringEncoding *coder = [GTMStringEncoding rfc4648Base64StringEncoding];
NSString *encodedBase64 = [coder encodeString:@"Mary had a little lamb"];

// will contain the original text
NSString *decodedText = [coder decodeString:encodedBase64];

To encode NSData* to NSString* and back to NSData*, use the encode: + decode: methods instead of encodeString: + decodeString:.

As a bonus you get a lot of additional useful encodings, such as the url-safe variant of Base64.

orip
  • 73,323
  • 21
  • 116
  • 148