class Radix::Base
Radix::Base
is a functional model of a base system that can be used for number conversions.
Radix::Base
is the original Radix
API. But with the advent of v2.0 and the new Integer
and Float
classes, it is essentially deprecated.
@example Convert any base
b10 = Radix::Base.new(10) b10.convert_base([100, 10], 256) #=> [2,5,6,1,0]
@example Convert to string notation upto base 62
b10.convert("10", 62) #=> "62"
@example Odd notations
b10 = Radix::Base.new(%w{Q W E R T Y U I O U}) b10.convert("FF", 16) #=> "EYY"
@!attribute [r] chars
@return [Array<String>] The ASCII character set in use.
@!attribute [r] base
@return [Fixnum] The base level in use.
@!attribute [r] values
@example Testing base hash default values. > test = Radix::Base.new(36) > test.values["F"] 15 > test.values["5"] 5 > test.values["Y"] 34 > test.values["YY"] nil # Fails because "YY" is not a key in the +values+ hash. @return [Hash{String=>Fixnum}] A hash of characters and their respective value.
Attributes
The base of this instance.
@return [Fixnum] The base level in use.
The characters for this base level.
@return [Array<String>] The ASCII character set in use.
Hash of characters and values.
@return [Hash{String=>Fixnum}]
A hash of characters and their respective value.
Public Class Methods
New Radix
using chars
representation.
@param [Array<String>, Numeric] chars
Array of string representation of number values of the base or a Numeric of the Base level.
@return [void]
# File lib/radix/base.rb, line 88 def initialize(chars=BASE::B62) if ::Numeric === chars chars = BASE::B62[0...chars] end @chars = chars.map{ |c| c.to_s } @base = @chars.size @values = Hash[*(0...@base).map { |i| [ @chars[i], i ] }.flatten] end
Public Instance Methods
Convert a value of given radix_base to that of the base instance.
@param [String, Numeric
, Array
<String>] number
The value in "radix_base" context.
@param [Radix::Base, Numeric] radix_base
Numeric for the radix or instance of Radix::Base.
@example Convert Testing (Binary, Decimal, Hex)
> b = Radix::Base.new(2) > d = Radix::Base.new(10) > h = Radix::Base.new(16) > d.convert("A", h) "10" > h.convert("A", d) TypeError > h.convert(10, d) "A" > h.convert(10, 10) "A" > b.convert(10, d) "1010" > b.convert(10, h) "10000"
@return [String] representation of “number” in self.base level.
# File lib/radix/base.rb, line 124 def convert(number, radix_base) radix_base = Radix::Base.new(radix_base) unless Radix::Base === radix_base case number when ::String, ::Numeric digits = number.to_s.split(//) else digits = number end # decode the digits digits = digits.map{ |digit| radix_base.values[digit] } # THINK: Is best way to check for base out of bounds? raise TypeError if digits.any?{ |digit| digit.nil? } digits = Radix.convert_base(digits, radix_base.base, base) digits = digits.map{ |digit| chars[digit] } digits.join end
Convert any base to any other base, using array of Fixnum’s. Indexes of the array correspond to values for each column of the number in from_base
@param [Array<Fixnum>] digits
Array of values for each digit of source base.
@param [Fixnum] from_base
Source Base
@param [Fixnum] to_base
Destination Base
@return [String] The value of digits in from_base converted as to_base.
# File lib/radix/base.rb, line 158 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 << 0 if converted.empty? # THINK: correct? converted.reverse end
Decode a string that was previously encoded in the radix.
@param [String] encoded
Encoded string from this Base.
@return [String] Decoded string of value from this base.
# File lib/radix/base.rb, line 190 def decode(encoded) digits = encoded.split(//).map{ |c| @values[c] } Radix.convert_base(digits, base, 256).pack("C*") end
Encode a string in the radix.
@param [String] byte_string
String value in this base.
@return [String] Encoded string from this Base
.
# File lib/radix/base.rb, line 177 def encode(byte_string) digits = byte_string.unpack("C*") digits = Radix.convert_base(digits, 256, base) digits.map{ |d| @chars[d] }.join end