Section 2.38. Encoding and Decoding Strings (uuencodeuudecode)


2.37. Encoding and Decoding base64 Strings

base64 is frequently used to convert machine-readable data into a text form with no special characters in it. For example, newsgroups that handle binary files such as program executables frequently will use base64.

The easiest way to do a base64 encode/decode is to use the built-in features of Ruby. The Array class has a pack method that returns a base64 string (given the parameter "m"). The String class has a method unpack that likewise unpacks the string (decoding the base64):

str = "\007\007\002\abdce" new_string = [str].pack("m")         # "BwcCB2JkY2U=" original   =  new_string.unpack("m") # ["\a\a\002\abdce"]


Note that an array is returned by unpack.




The Ruby Way(c) Solutions and Techniques in Ruby Programming
The Ruby Way, Second Edition: Solutions and Techniques in Ruby Programming (2nd Edition)
ISBN: 0672328844
EAN: 2147483647
Year: 2004
Pages: 269
Authors: Hal Fulton

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net