Module: BaseConvert::Functions

Included in:
FromTo, Number
Defined in:
lib/base_convert/functions.rb

Instance Method Summary collapse

Instance Method Details

#to_base(integer, base, digits) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/base_convert/functions.rb', line 14

def to_base(integer, base, digits)
  return digits[0] if integer == 0
  string = ''
  while integer > 0
    integer, index = integer.divmod(base)
    string = string.insert(0, digits[index])
  end
  string
end

#to_integer(string, base, digits) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/base_convert/functions.rb', line 5

def to_integer(string, base, digits)
  integer = 0
  string.each_char do |c|
    index = digits.index(c)
    integer = integer * base + index
  end
  integer
end