sja91

base64

Jan 6th, 2017
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.44 KB | None | 0 0
  1.  
  2. alphabet = ("A".."Z").to_a + ("a".."z").to_a + (0..9).to_a + ["/"] + ["+"]
  3.  
  4. str = "test222222222222"
  5. output = ""
  6. bits = ""
  7.  
  8. str.each_byte do |c|
  9. bitstr = c.to_s(2).rjust(8, "0")
  10. bits += bitstr #8 bits long
  11. end
  12.  
  13. bits.scan( /.{1,6}/).each do |x|
  14. index = x.ljust(6, "0").to_i(2)
  15. output += alphabet[ index]
  16. end
  17.  
  18. unless output.length % 4 == 0
  19. pad = (output.length / 4 + 1) * 4
  20. output.ljust( pad, "=")
  21. else
  22. output
  23. end
Advertisement
Add Comment
Please, Sign In to add comment