Raymond
Raymond

Reputation: 3670

Rubymotion Base64

I am looking for a simple way to encode a string using base64. In ruby motion I can't just use the Base64encode of Ruby because I am not able to require it. So I thought I could use a build in function of Cocoa. But Cocoa does not seem to have a Base64encode function. I have found some categories on NSData, but don't know how to use them in a ruby motion project. Should I create a statics library for this?

I have got the feeling that I am looking in the wrong direction, there must be easy solution for this?

Upvotes: 2

Views: 645

Answers (1)

Dylan Markow
Dylan Markow

Reputation: 124419

If you look at the source for Base64.encode64 method, you'll see that it just uses the pack method. So you can encode/decode like this (note that you need to put the thing you want to encode inside an array):

["my string"].pack("m")
# => "bXkgc3RyaW5n\n"

"bXkgc3RyaW5n\n".unpack("m").first
# => "my string"

http://www.ruby-doc.org/stdlib-1.9.3/libdoc/base64/rdoc/Base64.html#method-i-encode64

Upvotes: 8

Related Questions