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, #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



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

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.chop! 
  else
    val.chop! if length.odd?
  end
  f.set_value(val)
  return f,buf[l,buf.length]
end

#encode(field) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/codec/packed.rb', line 70

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