class Base31encoder
This module is used to convert a base10 integer into an equivalent base31 guid string (and vice-versa). The base31 number is represented as a alpha-numeric characters (excluding the vowel charatcers A, E, I, O, U); this is done to avoid confusion between numbers and letters (eg. 1 and I, 0 and O) and also to avoid nasty words from being generated from their equivalent decimal values.
Constants
- Dec_vals
base31/base10 equivalent string definitions
Public Class Methods
new(base_length=6)
click to toggle source
# File lib/base31_encoder.rb, line 21 def initialize(base_length=6) @base_length = base_length end
Public Instance Methods
base31str_to_int(str)
click to toggle source
# File lib/base31_encoder.rb, line 25 def base31str_to_int(str) if not str.is_a?(String) raise StandardError, "Received value is not a valid string" end len = pos = str.length total = 0 while pos > 0 do val = str[(len - pos), 1] intval = Dec_vals[val].to_i tmp = pos while (tmp - 1) > 0 do intval = intval * 31 tmp -= 1 end total += intval pos -= 1 end return total end
int_to_base31(num)
click to toggle source
# File lib/base31_encoder.rb, line 50 def int_to_base31(num) if not num.is_a?(Integer) raise StandardError, "Received value is not a valid integer" end base31 = "" while num > 0 do rem = num % 31 num = num / 31 val = Dec_vals[rem.to_s] base31 = val + base31 end while base31.length < @base_length do base31 = "0" + base31 end return base31 end