2

I'm trying to decode some data which I encoded in bash using

base64 -in file.txt -out encodedFile.txt

Decoding it in bash is no problem.

But when I try to decode it in Objective-C using

//fetch encoded data from file system, put it in
//NSData *encoded
NSData *decoded = [[NSData alloc] initWithBase64EncodedData:encoded withOptions:0];

I'm always receiving nil.

EDIT

I tried encoding the same file in Objective-C and noticed there is only one small difference:

Encoded in bash:

dGhpcyBpcyBhIHRlc3Qh

Encoded in Objective-C:

VGhpcyBpcyBhIHRlc3Qh

Only the first character is different.

jww
  • 97,681
  • 90
  • 411
  • 885
SnyersK
  • 1,296
  • 8
  • 23
  • 1
    Please add a reason for the downvote, so I can adjust the way I ask a question. – SnyersK Jul 10 '15 at 13:14
  • Also see [Base64 Decoding in iOS 7+](http://stackoverflow.com/a/22432808/608639) and Tommy's answer for another way to do it that works on all iOS devices. – jww Jul 11 '15 at 01:16
  • @jww I actually came across that when searching for a solution to my problem. But I only need to support iOS 7 and up, so no problem there. thanks! – SnyersK Jul 12 '15 at 23:49

1 Answers1

3

I figured it out.

Apparently there was a newline (\n) appended to the end of the file. Once I removed that, it worked normally.

For those who are interested, I managed to do this in one command using

base64 -i file.txt | tr -d '\n' > encodedFile.txt
SnyersK
  • 1,296
  • 8
  • 23