# Newbie: unpacking binary data

**URL:** <https://www.ruby-forum.com/t/newbie-unpacking-binary-data/79343>\
**Category:** Ruby\
**Created:** [December 14, 2006, 12:14pm UTC](https://www.ruby-forum.com/t/newbie-unpacking-binary-data/79343 "2006-12-14T12:14:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [December 14, 2006, 12:14pm UTC](https://www.ruby-forum.com/t/newbie-unpacking-binary-data/79343/1 "2006-12-14T12:14:02Z")

</div>

Hi,  
Thanks for the efforts put into making Ruby available!  
While new to Ruby it seems worth investing some time in. Eventually I’d  
like to unpack some data from a binary file and insert it into a MySQL  
table.  
At the moment I’m having trouble looping over the source data file, and  
just viewing the data to make sure things are proceeding correctly.

FYI: eventually I need this to run quickly (many GB’s to be processed),  
but for the moment I’ll settle for getting anything to work 🙂

The ruby details are:  
$ ruby -v  
ruby 1.8.5 (2006-08-25) [i386-cygwin]

* * *

## Attempt 1 ( an error )

File.foreach("./INPUT/T200510A.BIN", “rb”) do |f|  
print f.read(19).unpack(‘I4 I4 I4 I2 I2 a2 a1’)  
end  
NoMethodError: [FATAL] failed to allocate memory

* * *

## Attempt 2 ( an error )

File.open("./INPUT/T200510A.BIN", “rb”).readlines do |f|  
data = f.read(19).unpack(‘I4 I4 I4 I2 I2 a2 a1’)  
print data  
end  
/usr/lib/ruby/1.8/irb.rb:298:in `inspect': failed to allocate memory (NoMemoryEr ror) from /usr/lib/ruby/1.8/irb.rb:298:in`output\_value’  
from /usr/lib/ruby/1.8/irb.rb:151:in `eval_input' from /usr/lib/ruby/1.8/irb.rb:259:in`signal\_status’  
from /usr/lib/ruby/1.8/irb.rb:147:in `eval_input' from /usr/lib/ruby/1.8/irb/ruby-lex.rb:244:in`each\_top\_level\_statement’

```
    from /usr/lib/ruby/1.8/irb/ruby-lex.rb:230:in `loop'
    from /usr/lib/ruby/1.8/irb/ruby-lex.rb:230:in

```

`each\_top\_level\_statement’

```
    from /usr/lib/ruby/1.8/irb/ruby-lex.rb:229:in `catch'
    from /usr/lib/ruby/1.8/irb/ruby-lex.rb:229:in

```

`each\_top\_level\_statement’

```
    from /usr/lib/ruby/1.8/irb.rb:146:in `eval_input'
    from /usr/lib/ruby/1.8/irb.rb:70:in `start'
    from /usr/lib/ruby/1.8/irb.rb:69:in `catch'
    from /usr/lib/ruby/1.8/irb.rb:69:in `start'
    from /usr/bin/irb:13
```

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [December 14, 2006, 1:21pm UTC](https://www.ruby-forum.com/t/newbie-unpacking-binary-data/79343/2 "2006-12-14T13:21:32Z")

</div>

On 14.12.2006 12:14, Mark Van de vyver wrote:

> NoMethodError: [FATAL] failed to allocate memory  
> This does not work because the second argument to foreach is the /line  
> separator/ - not the file mode.

> * * *
> 
> ## Attempt 2 ( an error )
> 
> File.open("./INPUT/T200510A.BIN", “rb”).readlines do |f|  
> data = f.read(19).unpack(‘I4 I4 I4 I2 I2 a2 a1’)  
> print data  
> end  
> /usr/lib/ruby/1.8/irb.rb:298:in `inspect’: failed to allocate memory  
> (NoMemoryError)

This fails because you use readlines - this will try to read the whole  
file into memory as /lines/ - and since there are no lines in a binary  
file this chokes while attempting to store the whole file or a large  
portion of it in a single line (i.e. String).

You should do this

File.open("./INPUT/T200510A.BIN", “rb”) do |io|  
while ( data = io.read(19) )  
p data.unpack(‘I4I4I4I2I2a2a1’)  
end  
end

Regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![markcasio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9bcb4/32.png) [@markcasio](https://www.ruby-forum.com/u/markcasio)\
**Post date:** [December 15, 2006, 8:32am UTC](https://www.ruby-forum.com/t/newbie-unpacking-binary-data/79343/3 "2006-12-15T08:32:14Z")

</div>

Robert K. wrote:

> Regards
> 
> robert

Thanks Robert. Much appreciated.
