module Ronin::Support::Encoding::Base62
Base62
encoding.
## Core-Ext Methods
-
{Integer#base62_encode}
-
{String#base62_decode}
@see en.wikipedia.org/wiki/Base62
@api public
@since 1.1.0
Constants
- TABLE
-
Base62
table
Public Class Methods
Source
# 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
Base62
decodes a String
back into an Integer
.
@param [String] string
The string to Base62 decode.
@return [Integer]
The Base62 decoded integer.
Source
# 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
Base62
encodes an integer.
@param [Integer] int
The integer to Base62 encode.
@return [String]
The Base62 encoded string.