Class: Codec::Numpck

Inherits:
Packed show all
Defined in:
lib/codec/packed.rb

Instance Attribute Summary

Attributes inherited from Base

#id

Instance Method Summary collapse

Methods inherited from Packed

#decode, #encode_with_length, #get_length, #get_pck_length

Methods inherited from Base

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

Constructor Details

This class inherits a constructor from Codec::Base

Instance Method Details

#decode_with_length(buf, length) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/codec/packed.rb', line 30

def decode_with_length(buf,length)
  l = eval_length(buf,get_pck_length(length))
  f = Field.new(@id)
  val = buf[0,l].unpack("H*").first
  if @length.odd?
    val = val[1,val.length]
  else
    val = val[1,val.length] if length.odd?
  end
  f.set_value(val.to_i)
  return f,buf[l,buf.length]
end

#encode(field) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/codec/packed.rb', line 43

def encode(field)
  out = field.get_value.to_s
  if @length > 0
    out = out.rjust(@length,"0")
    raise TooLongDataException if out.length > @length
  end
  l = out.length
  out.prepend("0") if out.length.odd?
  out = [out].pack("H*")
  return out
end