Class: Codec::Strpck

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, #decode_with_length, #encode_with_length, #get_length, #get_pck_length

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

Instance Method Details

#build_field(buf, length) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/codec/packed.rb', line 50

def build_field(buf,length)
  f = Field.new(@id)
  val = buf[0,length].unpack("H*").first
  # TODO : handle odd length for packed field with prefixed length
  val.chop! if @length.odd?
  f.set_value(val)
  return f
end

#encode(field) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/codec/packed.rb', line 59

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