-
-
Save alloy/2786823 to your computer and use it in GitHub Desktop.
NSDATA+Base64 wrapper UTF8 issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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)?
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
It might be a good idea to file a support ticket about this. Mind doing that?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Still garbage:
Base64.encode64('Send reinforcements')
=> "4BidBgwAAAAAAAAAAAAAAIDI1A=="