# Reading binary file written by C program

**URL:** <https://www.ruby-forum.com/t/reading-binary-file-written-by-c-program/89798>\
**Category:** Ruby\
**Created:** [March 22, 2007, 8:28pm UTC](https://www.ruby-forum.com/t/reading-binary-file-written-by-c-program/89798 "2007-03-22T20:28:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yacao\_Wang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/bcef8e/32.png) [@Yacao\_Wang](https://www.ruby-forum.com/u/Yacao_Wang)\
**Post date:** [March 22, 2007, 8:28pm UTC](https://www.ruby-forum.com/t/reading-binary-file-written-by-c-program/89798/1 "2007-03-22T20:28:17Z")

</div>

Hi all,

I’ve learned some basic of Ruby and I need to write a program that can  
read  
data from binary files written by a C program. I was supposed to use the  
company library to do this using C++ but I really feel that this job  
could  
be done more quickly with the advanced features of Ruby. The binary  
files  
are generally directly dumped by C structs. Is there a “ruby way” of  
doing  
this? Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Yacao\_Wang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/bcef8e/32.png) [@Yacao\_Wang](https://www.ruby-forum.com/u/Yacao_Wang)\
**Post date:** [March 22, 2007, 8:37pm UTC](https://www.ruby-forum.com/t/reading-binary-file-written-by-c-program/89798/2 "2007-03-22T20:37:18Z")

</div>

I would start here:  
[http://raa.ruby-lang.org/search.rhtml?search=binary](http://raa.ruby-lang.org/search.rhtml?search=binary)  
(Also, check on [rubyforge.org](http://rubyforge.org))

You might want to look at Array#pack and String#unpack.  
Also, make sure to open up the file as binary!

Enjoy.

---

<div class="post-metadata">

**Author:** ![Yacao\_Wang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/bcef8e/32.png) [@Yacao\_Wang](https://www.ruby-forum.com/u/Yacao_Wang)\
**Post date:** [March 22, 2007, 10:02pm UTC](https://www.ruby-forum.com/t/reading-binary-file-written-by-c-program/89798/3 "2007-03-22T22:02:30Z")

</div>

On 3/22/07, Paul S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I would start here:  
> [http://raa.ruby-lang.org/search.rhtml?search=binary](http://raa.ruby-lang.org/search.rhtml?search=binary)  
> (Also, check on [rubyforge.org](http://rubyforge.org))

in there you will see the binary-parse gem, I wrote it to do exactly  
what you are describing for my own needs.

pth

---

<div class="post-metadata">

**Author:** ![Yacao\_Wang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/bcef8e/32.png) [@Yacao\_Wang](https://www.ruby-forum.com/u/Yacao_Wang)\
**Post date:** [March 22, 2007, 10:05pm UTC](https://www.ruby-forum.com/t/reading-binary-file-written-by-c-program/89798/4 "2007-03-22T22:05:32Z")

</div>

On Fri, 23 Mar 2007, Yacao Wang wrote:

> Hi all,
> 
> I’ve learned some basic of Ruby and I need to write a program that can read  
> data from binary files written by a C program. I was supposed to use the  
> company library to do this using C++ but I really feel that this job could  
> be done more quickly with the advanced features of Ruby. The binary files  
> are generally directly dumped by C structs. Is there a “ruby way” of doing  
> this? Thanks in advance.

look for joel’s bitstruct on rubyforge

-a

---

<div class="post-metadata">

**Author:** ![Yacao\_Wang](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/bcef8e/32.png) [@Yacao\_Wang](https://www.ruby-forum.com/u/Yacao_Wang)\
**Post date:** [March 22, 2007, 10:49pm UTC](https://www.ruby-forum.com/t/reading-binary-file-written-by-c-program/89798/5 "2007-03-22T22:49:43Z")

</div>

On 3/22/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> look for joel’s bitstruct on rubyforge
> 
> ## -a
> 
> be kind whenever possible… it is always possible.
> 
> - the dalai lama

Which is a fast, better solution for simple structures, binary-parse  
can handle very complex nested structures, arrays and the like.

pth
