We Recommend

Beginning Ruby: From Novice to Professional Beginning Ruby: From Novice to Professional
Beginning Ruby is a thoroughly contemporary guide for every type of reader wanting to learn Ruby, from novice programmers to web developers to Ruby newcomers. It starts by explaining the principles behind object-oriented programming and within a few chapters builds toward creating a genuine Ruby application.


Posted By

szsk on 06/05/08


Tagged

ruby base64


Versions (?)


Who likes this?

1 person has marked this snippet as a favorite

banzaiman


base64 enocde/decode


Published in: Ruby 


  1. def decode_base64( text )
  2. text.unpack( "m" )[0]
  3. end
  4.  
  5. def encode_base64( bin )
  6. [ bin ].pack( "m" ).gsub( /\s/, "" )
  7. end
  8.  
  9.  
  10.  
  11. text = "The quick brown fox jumps over the lazy dog"
  12.  
  13. p encode_base64( text )
  14. # => VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZw==
  15. p decode_base64( encode_base64( text ) )
  16. # => The quick brown fox jumps over the lazy dog

Report this snippet 

You need to login to post a comment.