Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to unpack unusual binary structure?

Reply
Thread Tools

How to unpack unusual binary structure?

 
 
Leo --
Guest
Posts: n/a
 
      12-07-2006
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 * 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/.

 
Reply With Quote
 
 
 
 
Farrel Lifson
Guest
Posts: n/a
 
      12-07-2006
On 07/12/06, Leo -- <(E-Mail Removed)> 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 * 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

 
Reply With Quote
 
 
 
 
Leo --
Guest
Posts: n/a
 
      12-07-2006
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/.

 
Reply With Quote
 
Farrel Lifson
Guest
Posts: n/a
 
      12-07-2006
On 07/12/06, Leo -- <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
(8-bit binary to two digit bcd) or (8-bit binary to two digit seven segment) Fangs VHDL 3 10-26-2008 06:41 AM
Unpack binary files. dave Ruby 7 08-07-2005 12:51 PM
Unpack binary file with C Struct to text? RT Perl Misc 2 03-07-2004 01:19 PM
Re: ostreams, ios::binary, endian, mixed binary-ascii Marc Schellens C++ 8 07-15-2003 12:27 PM



Advertisments