Class: Codec::Numbin

Inherits:
Base
  • Object
show all
Defined in:
lib/codec/fix.rb

Instance Attribute Summary

Attributes inherited from Base

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#add_sub_codec, #decode, #decode_with_length, #encode_with_length, #eval_length, #get_length, #get_sub_codecs, #initialize

Constructor Details

This class inherits a constructor from Codec::Base

Class Method Details

.numbin(number, maxlength) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/codec/fix.rb', line 20

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, length) ⇒ Object



3
4
5
6
7
8
9
10
11
12
# File 'lib/codec/fix.rb', line 3

def build_field(buf,length)
  f = Field.new(@id)
  res = 0
  buf[0,length].unpack("C*").each{ |ubyte|
    res *= 256
    res += ubyte
  }
  f.set_value(res)
  return f
end

#encode(field) ⇒ Object



14
15
16
17
18
# File 'lib/codec/fix.rb', line 14

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