def encode(value)
      begin
        encoded_value = codec.encode(value) 
      rescue ISO8583Exception => e
        ContextLog.exception(e, e.backtrace, "#{e.message} (#{name})")
        raise ISO8583Exception.new(e.message+" (#{name})")
      end

      if padding
        if padding.arity == 1
          encoded_value = padding.call(encoded_value)
        elsif padding.arity == 2
          encoded_value = padding.call(encoded_value, length)
        end
      end

      len_str = case length
                when Integer
                  raise ISO8583Exception.new("Too long: #{value} (#{name})! length=#{length}")  if encoded_value.length > length
                  raise ISO8583Exception.new("Too short: #{value} (#{name})! length=#{length}") if encoded_value.length < length
                  "" 
                when Field
                  raise ISO8583Exception.new("Max lenth exceeded: #{value}, max: #{max}") if max && encoded_value.length > max
                  length.encode(encoded_value.length)
                else
                  raise ISO8583Exception.new("Invalid length (#{length}) for '#{name}' field")
                end

      len_str + encoded_value
    end
def encode(value)
      begin
        encoded_value = codec.encode(value) 
      rescue ISO8583Exception => e
        ContextLog.exception(e, e.backtrace, "#{e.message} (#{name})")
        raise ISO8583Exception.new(e.message+" (#{name})")
      end

      if padding
        if padding.arity == 1
          encoded_value = padding.call(encoded_value)
        elsif padding.arity == 2
          encoded_value = padding.call(encoded_value, length)
        end
      end

      len_str = case length
                when Integer
                  raise ISO8583Exception.new("Too long: #{value} (#{name})! length=#{length}")  if encoded_value.length > length
                  raise ISO8583Exception.new("Too short: #{value} (#{name})! length=#{length}") if encoded_value.length < length
                  "" 
                when Field
                  raise ISO8583Exception.new("Max lenth exceeded: #{value}, max: #{max}") if max && encoded_value.length > max
                  length.encode(encoded_value.length)
                else
                  raise ISO8583Exception.new("Invalid length (#{length}) for '#{name}' field")
                end

      len_str + encoded_value
    end
Encoding needs to consider length representation, the actual encoding (such as charset or BCD)
 and padding.
 The order may be important! This impl calls codec.encode and then pads, in case you need the other
 special treatment, you may need to override this method alltogether.
 In other cases, the padding has to be implemented by the codec, such as BCD with an odd number of nibbles.