Support Requests: Browse | Submit New | Admin

[#29663] Trying to add primitive type

Date:
2012-11-06 05:43
Priority:
3
Submitted By:
Len Sumnler (lsumnler)
Assigned To:
Dion Mendel (dmendel)
Category:
None
State:
Closed
Summary:
Trying to add primitive type

Detailed description
Dear Sir

I have been trying to figure this out for several day and have posted on Stackoverflow and reddit and received no answers
so I now come to you directly.

I am using your gem BinData but would like to create a primitive type which I call "pkddec" => packed decimal
similar to a COMP-3 in cobol.  I have created a program which I have attached which works using a "string"
primitive and conversion code but would prefer to use a user defined primitive as follows

class WangRec < BinData::Record
  ...
  pkddec  :amt1  :read_length => 6  :dec_pos => 2
  ...
end

Any help would be appreciated.

Thanks
Len

Add A Comment: Notepad

Please login


Followup

Message
Date: 2012-11-06 09:45
Sender: Dion Mendel

Here's the framework to use. You just need to insert the pack/unpack
code.

class PkdDec < BinData::Primitive
  mandatory_parameter :length
  default_parameter   :dec_pos => 0
  
  string :str, :read_length => :length
  
  def get
    str_length = eval_parameter(:length)
    dec_pos    = eval_parameter(:dec_pos)

    unpacked   = str.unpack("H#{str_length
* 2}").first
    
    dec_val = # convert unpacked value to float ...

    dec
  end

  def set(dec_val)
    packed_str = # convert dec_val to packed string ...

    str.assign(packed_str)
  end
end

class MyRecord < BinData::Record
  pkd_dec :amt1, :length => 6, :dec_pos => 2
end

Attached Files:

Name Description Download
read_aropnfil.rb sample code that works Download

Changes:

Field Old Value Date By
close_date2013-05-17 02:122013-05-17 02:12dmendel
status_idOpen2013-05-17 02:12dmendel
assigned_tonone2012-11-06 09:45dmendel
File Added5884: read_aropnfil.rb2012-11-06 05:43lsumnler