module Ronin::Support::Encoding::Base62

Base62

encoding.

[Base62]: en.wikipedia.org/wiki/Base62

## Core-Ext Methods

@api public

@since 1.1.0

Constants

TABLE

Base62 table

Public Class Methods

decode(string) click to toggle source

Base62 decodes a String back into an Integer.

@param [String] string

The string to Base62 decode.

@return [Integer]

The Base62 decoded integer.
# 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) click to toggle source

Base62 encodes an integer.

@param [Integer] int

The integer to Base62 encode.

@return [String]

The Base62 encoded string.
# 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