How to unpack unusual binary structure?

Discussion in 'Ruby' started by Leo --, Dec 7, 2006.

  1. Leo --

    Leo -- Guest

    Hello everyone

    I try to read binary file and it has section in following format:
    (I suspect this comes from .h file)

    ...
    then the bars array (one-byte justification) follows

    pragma pack(push,1)
    //---- Standard representation of a quote in the base
    struct RateInfo {
    time_t ctm; // current time in seconds
    double open;
    double low;
    double high;
    double close;
    double vol;
    };
    pragma pack(pop)
    ...

    I don't know what to do with pragma pack. I googled and found that this
    is some weird bit-level alignment for records. But I can't figure out,
    how do I read and .unpack such records.

    Can I just do .read(4 + 5 * 8) and get a record?
    How do I decode it if its packed with this pragma pack?

    Need help...

    --
    Posted via http://www.ruby-forum.com/.
    Leo --, Dec 7, 2006
    #1
    1. Advertising

  2. On 07/12/06, Leo -- <> wrote:
    > Hello everyone
    >
    > I try to read binary file and it has section in following format:
    > (I suspect this comes from .h file)
    >
    > ...
    > then the bars array (one-byte justification) follows
    >
    > pragma pack(push,1)
    > //---- Standard representation of a quote in the base
    > struct RateInfo {
    > time_t ctm; // current time in seconds
    > double open;
    > double low;
    > double high;
    > double close;
    > double vol;
    > };
    > pragma pack(pop)
    > ...
    >
    > I don't know what to do with pragma pack. I googled and found that this
    > is some weird bit-level alignment for records. But I can't figure out,
    > how do I read and .unpack such records.
    >
    > Can I just do .read(4 + 5 * 8) and get a record?
    > How do I decode it if its packed with this pragma pack?
    >
    > Need help...
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >


    Have you looked at String#unpack ?

    Farrel
    Farrel Lifson, Dec 7, 2006
    #2
    1. Advertising

  3. Leo --

    Leo -- Guest

    Farrel Lifson wrote:
    > Have you looked at String#unpack ?


    Yes, but I do not know how to apply it in the case of packed data
    structure. I'm not much into CS, so if this is possible with
    String#unpack I will be very happy if you can help me with format string
    to unpack this "pragma pack(1)" data structure.

    --
    Posted via http://www.ruby-forum.com/.
    Leo --, Dec 7, 2006
    #3
  4. On 07/12/06, Leo -- <> wrote:
    > Farrel Lifson wrote:
    > > Have you looked at String#unpack ?

    >
    > Yes, but I do not know how to apply it in the case of packed data
    > structure. I'm not much into CS, so if this is possible with
    > String#unpack I will be very happy if you can help me with format string
    > to unpack this "pragma pack(1)" data structure.
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >


    This is just a guess and I'm not sure what time_t is but if we assuume
    it's an unsigned int then you could say:
    ctm,open,low,high,close,vol = packedData.unpack("Iddddd")

    Farrel
    Farrel Lifson, Dec 7, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Duke of Hazard

    newbie: unusual email output

    Duke of Hazard, Jan 21, 2004, in forum: Perl
    Replies:
    4
    Views:
    731
  2. William A. Sempf

    Unusual performance problem.

    William A. Sempf, Aug 27, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    372
    William A. Sempf
    Aug 27, 2003
  3. Frederiek
    Replies:
    3
    Views:
    395
    Jakob Bieling
    Jul 11, 2006
  4. dave

    Unpack binary files.

    dave, Aug 7, 2005, in forum: Ruby
    Replies:
    7
    Views:
    172
    Erik Veenstra
    Aug 7, 2005
  5. RT
    Replies:
    2
    Views:
    223
Loading...

Share This Page