Skip to content

Instantly share code, notes, and snippets.

@QaDeS
Created May 11, 2011 21:46
Show Gist options
  • Save QaDeS/967450 to your computer and use it in GitHub Desktop.
Save QaDeS/967450 to your computer and use it in GitHub Desktop.
Base105 Encoding using all ASCII printable characters
module Base105
EncChars = (' '..'~').to_a.flatten
DecChars = begin
dec = []
EncChars.each_with_index{ |c, i| dec[c.ord] = i }
dec
end
def self.encode(i)
result = ''
while (i, j = i.divmod(EncChars.size)) != [0,0]
result << EncChars[j]
end
result.reverse
end
def self.decode(s)
result = 0
s.each_char do |c|
result = result * EncChars.size + DecChars[c.ord]
end
result
end
end
# usage:
# i=4371937102758371285789341768594106489305974329150
# puts i
# puts j=Base105.encode(i)
# puts Base105.decode(j)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment