Class | BaseX |
In: |
lib/more/facets/basex.rb
|
Parent: | Object |
BASE62 | = | ["0".."9", "a".."z", "A".."Z"].map { |r| r.to_a }.flatten |
chars | [RW] |
# File lib/more/facets/basex.rb, line 7 def initialize(chars=BASE62) @chars = chars @base = @chars.size @values = Hash[*(0...@base).map { |i| [ @chars[i], i ] }.flatten] end
# File lib/more/facets/basex.rb, line 25 def convert_base(digits, from_base, to_base) bignum = 0 digits.each { |digit| bignum = bignum * from_base + digit } converted = [] until bignum.zero? bignum, digit = bignum.divmod to_base converted.push digit end converted.reverse end
# File lib/more/facets/basex.rb, line 19 def decode(encoded) convert_base(encoded.split('').map { |c| @values[c] }, @base, 256).pack("C*") end