Skip to content

Instantly share code, notes, and snippets.

@alloy
Forked from barttenbrinke/gist:2786800
Created May 25, 2012 09:04
Show Gist options
  • Save alloy/2786823 to your computer and use it in GitHub Desktop.
Save alloy/2786823 to your computer and use it in GitHub Desktop.
NSDATA+Base64 wrapper UTF8 issue
class Base64
def self.encode64(string)
result = NSData.alloc.initWithData(string).base64EncodedString
result = result.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion:true)
NSString.alloc.initWithData(result, encoding:NSUTF8StringEncoding)
end
def self.decode64(string)
data = NSData.dataFromBase64String(string)
NSString.alloc.initWithData(data, encoding:NSUTF8StringEncoding)
end
end
@barttenbrinke
Copy link

Still garbage:

Base64.encode64('Send reinforcements')
=> "4BidBgwAAAAAAAAAAAAAAIDI1A=="

@alloy
Copy link
Author

alloy commented May 25, 2012

Poo.

Maybe it has to do with the fact that we first let it create the ASCII representation, is there maybe another method that only returns the data and then we create the string ourselves (using UTF8)?

@barttenbrinke
Copy link

I think the core issue is in the fact that the UTF8 => ASCII casting is done implicitly, which fails. Off course I could rewrite the NSData+Base64 cocoapod, but I rather not want to do that. As this is not an issue for Base64 anymore (although using native ios should be faster then the string stuff of rubinius), I think it is ok.

However, I'm afraid i'll be bumping into the same issue for OpenSSL, which might make it impossible to do at all :X

@alloy
Copy link
Author

alloy commented May 25, 2012

It might be a good idea to file a support ticket about this. Mind doing that?

@barttenbrinke
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment