# How to unpack unusual binary structure?

**URL:** <https://www.ruby-forum.com/t/how-to-unpack-unusual-binary-structure/78622>\
**Category:** Ruby\
**Created:** [December 7, 2006, 10:59am UTC](https://www.ruby-forum.com/t/how-to-unpack-unusual-binary-structure/78622 "2006-12-07T10:59:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![leopardus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f9ae1b/32.png) [@leopardus](https://www.ruby-forum.com/u/leopardus)\
**Post date:** [December 7, 2006, 10:59am UTC](https://www.ruby-forum.com/t/how-to-unpack-unusual-binary-structure/78622/1 "2006-12-07T10:59:24Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![leopardus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f9ae1b/32.png) [@leopardus](https://www.ruby-forum.com/u/leopardus)\
**Post date:** [December 7, 2006, 11:06am UTC](https://www.ruby-forum.com/t/how-to-unpack-unusual-binary-structure/78622/2 "2006-12-07T11:06:08Z")

</div>

On 07/12/06, Leo – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> struct RateInfo {  
> I don’t know what to do with pragma pack. I googled and found that this

Have you looked at String#unpack ?

Farrel

---

<div class="post-metadata">

**Author:** ![leopardus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f9ae1b/32.png) [@leopardus](https://www.ruby-forum.com/u/leopardus)\
**Post date:** [December 7, 2006, 1:46pm UTC](https://www.ruby-forum.com/t/how-to-unpack-unusual-binary-structure/78622/3 "2006-12-07T13:46:38Z")

</div>

On 07/12/06, Leo – [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

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

---

<div class="post-metadata">

**Author:** ![leopardus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f9ae1b/32.png) [@leopardus](https://www.ruby-forum.com/u/leopardus)\
**Post date:** [December 7, 2006, 1:37pm UTC](https://www.ruby-forum.com/t/how-to-unpack-unusual-binary-structure/78622/4 "2006-12-07T13:37:44Z")

</div>

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