Module: Ronin::Support::Encoding::Base62
- Defined in:
- lib/ronin/support/encoding/base62.rb
Overview
Constant Summary collapse
- TABLE =
Base62 table
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
Class Method Summary collapse
-
.decode(string) ⇒ Integer
Base62 decodes a String back into an Integer.
-
.encode_int(int) ⇒ String
Base62 encodes an integer.
Class Method Details
.decode(string) ⇒ Integer
Base62 decodes a String back into an Integer.
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/ronin/support/encoding/base62.rb', line 76 def self.decode(string) decoded = 0 multiplier = 1 table_size = TABLE.length string.each_char.reverse_each.with_index do |char,index| decoded += TABLE.index(char) * multiplier multiplier *= table_size end return decoded end |
.encode_int(int) ⇒ String
Base62 encodes an integer.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/ronin/support/encoding/base62.rb', line 49 def self.encode_int(int) if int == 0 String.new('0') elsif int < 0 raise(ArgumentError,"cannot Base62 encode negative numbers: #{int.inspect}") else encoded = String.new table_size = TABLE.length while int > 0 encoded.prepend(TABLE[int % table_size]) int /= table_size end return encoded end end |