Skip to content

Instantly share code, notes, and snippets.

@jgn
Created November 20, 2010 02:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgn/707543 to your computer and use it in GitHub Desktop.
Save jgn/707543 to your computer and use it in GitHub Desktop.
rad50
#!/usr/bin/env ruby
# http://en.wikipedia.org/wiki/RADIX-50
class String
R50_CHARS = " ABCDEFGHIJKLMNOPQRSTUVWXYZ$.%0123456789"
R50_LOOKUP = {}.tap { |h| String::R50_CHARS.split(//).each_with_index { |c, i| h[c] = i } }
R50_CLEAN = /[[:upper:]|[:digit:]| |.|$|%]/
def radix50
[].tap do |r|
self.upcase.scan(R50_CLEAN).inject([]) do |m, c|
m << R50_LOOKUP[c]
end.each_slice(3) { |x, y, z| r << (((x || 0)*0o50 + (y || 0)) * 0o50 + (z || 0)).to_s(8) }
end
end
end
p 'Nothing sucks like a Vax.'.radix50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment