Class: Codec::Numbin

Inherits:
Fix show all
Defined in:
lib/codec/fix.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Fix

#check_length, #decode, #initialize

Methods inherited from Base

#add_sub_codec, #decode, #get_sub_codecs

Constructor Details

This class inherits a constructor from Codec::Fix

Class Method Details

.numbin(number, maxlength) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/codec/fix.rb', line 48

def self.numbin(number,maxlength)
  out = ""
  while number > 0
    out << (number % 256).chr
    number /= 256
  end
  
  # handle length if defined
  if maxlength > 0
    while out.length < maxlength
      out << 0.chr
    end
    out = out[0,maxlength]
  end
  out = 0.chr if out == ""
  return out.reverse
end

Instance Method Details

#build_field(buf, f, length) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/codec/fix.rb', line 32

def build_field(buf,f,length)
  res = 0
  buf.slice!(0...length).unpack("C*").each{ |ubyte|
    res *= 256
    res += ubyte
  }
  f.set_value(res)
end

#encode(buf, field) ⇒ Object



41
42
43
44
45
46
# File 'lib/codec/fix.rb', line 41

def encode(buf, field)
  val = field.get_value.to_i
  out = Numbin.numbin(val,@length)
  buf << out
  return out.length
end