class Ciphr::Functions::Radix::Radix

Public Class Methods

aligned() click to toggle source
# File lib/ciphr/functions/base_radix.rb, line 161
def self.aligned
  :right
end
params() click to toggle source
# File lib/ciphr/functions/base_radix.rb, line 169
def self.params
  [:input]
end
variants() click to toggle source
# File lib/ciphr/functions/base_radix.rb, line 165
def self.variants
  (2..36).map{|r| [["r#{r}","rad#{r}","radix#{r}"], {:radix => r}]}
end

Public Instance Methods

apply() click to toggle source
# File lib/ciphr/functions/base_radix.rb, line 173
def apply
  radix = options[:radix]
  input = @args[0]
  if !invert     
    num = 0      
    while chunk = input.read(1)
      num = (num << 8) + chunk.bytes.to_a[0]
    end
    Proc.new do
      begin
        num && num.to_s(radix)
      ensure
        num = nil
      end
    end
  else
    num = input.read().to_i(radix)
    bytes = []
    while num > 0
      bytes.unshift(num & 0xff)
      num = num >> 8
    end
    Proc.new do
      begin
        bytes && bytes.pack("c*")
      ensure
        bytes = nil
      end
    end
  end
end