0

Im using the following code to convert base64 string to ordinary string.

   NSError *localError = nil;
    NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:myString options:0];
    NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
    NSLog(@"encoded string , %@",myString);
    NSLog(@"Decode String Value: %@", decodedString);

Encoded string prints the base64 string but the decoded string is empty. Why so?

ThomasW
  • 16,981
  • 4
  • 79
  • 106

3 Answers3

2

Avoid converting to a string before decoding:

NSData *decodedData = [[NSData alloc] initWithBase64EncodedData:data options:NSDataBase64DecodingIgnoreUnknownCharacters];
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];

Some implementations of Base64 add line breaks every 64 characters. You should be able to address this by using this option: NSDataBase64DecodingIgnoreUnknownCharacters.

JoGoFo
  • 1,928
  • 14
  • 31
  • still the output is empty –  Jan 16 '17 at 09:37
  • Where is the base64 encoded content from? Are you sure it is valid? – JoGoFo Jan 16 '17 at 09:39
  • I think some implementations of Base64 add line breaks every 64 characters. You should be able to address this by using this option: `NSDataBase64DecodingIgnoreUnknownCharacters`. I have also seen some implementations which fail to pad to a multiple of 4 characters with `=` characters. See this answer for more info: http://stackoverflow.com/questions/21406482/nsdata-wont-accept-valid-base64-encoded-string – JoGoFo Jan 16 '17 at 10:25
  • im receiving "WwogICJVc2VyTG9naW5SZXNwb25zZVN1Y2Nlc3MiLCAKICB7CiAgICAic2Vzc2lvbl90b2tlbiI6ICIxNDY3YTAxMWRlZmI0MmEyODBiNWIyZWQ0NmRhYzFiYSIsIAogICAgInVzZXJfaWQiOiA4LCAKICAgICJlbXBsb3llZV9uYW1lIjogImtub3cgbWFuYWdlciIKICB9Cl0=" –  Jan 16 '17 at 10:36
  • Glad it helped, I've updated the answer with the bit that helped you out :) – JoGoFo Jan 16 '17 at 10:43
0

If you want to encode and decode data then you can use this code.

    // Create NSData object
    NSData *data1 =[@"My String" dataUsingEncoding:NSUTF8StringEncoding];

    // Encoded NSString from NSData
    NSString *base64Encoded = [data1 base64EncodedStringWithOptions:0];
    NSLog(@"%@",base64Encoded);

    // Encoding data
    NSData *base64Data = [data1 base64EncodedDataWithOptions:0];
    NSLog(@"%@",base64Data);

    // Decoding data
    NSData *nsdataDecoded = [base64Data initWithBase64EncodedData:base64Data options:0];
    NSString *str = [[NSString alloc] initWithData:nsdataDecoded encoding:NSUTF8StringEncoding];
    NSLog(@"%@", str);
Sunny
  • 821
  • 6
  • 17
  • it prints the same base64 string –  Jan 16 '17 at 09:37
  • the response from the server is already a base64 string so I used as following ` NSData *nsdataDecoded = [data initWithBase64EncodedData:data options:0]; NSString *str = [[NSString alloc] initWithData:nsdataDecoded encoding:NSUTF8StringEncoding];` –  Jan 16 '17 at 10:19
  • but the output is empty string –  Jan 16 '17 at 10:19
  • Please verify base64 you are receiving from server is correct. – Sunny Jan 16 '17 at 10:24
  • Im receiving "WwogICJVc2VyTG9naW5SZXNwb25zZVN1Y2Nlc3MiLCAKICB7CiAgICAic2Vzc2lvbl90b2tlbiI6ICIxNDY3YTAxMWRlZmI0MmEyODBiNWIyZWQ0NmRhYzFiYSIsIAogICAgInVzZXJfaWQiOiA4LCAKICAgICJlbXBsb3llZV9uYW1lIjogImtub3cgbWFuYWdlciIKICB9Cl0=" –  Jan 16 '17 at 10:36
-1

You can use Base64 library to encode or decode,

https://github.com/dasdom/hAppy/tree/master/base64

Then you can use this code,

NSString *strEncoded = [Base64 encode:data];