How to unpack unusual binary structure?

L

Leo --

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...
 
F

Farrel Lifson

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...

Have you looked at String#unpack ?

Farrel
 
L

Leo --

Farrel said:
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.
 
F

Farrel Lifson

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.

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top