42

Is there a simple way to write binary data into binary file like we used to do in C/C++? For example, how can I create a 4-byte file with serialized 4-byte integer value without using fancy math?

5 Answers 5

37

You can use Array#pack and String#unpack to convert to and from binary representations. Combine them with IO#write and IO#read, and away you go.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I think yours is the better answer for this particular question! +1
This seems like what I want to do, but I still don't know how to do it. What format should I unpack a binary string to using String#unpack ?
33

I recently had a similar problem for work. I used the BinData gem and it worked a treat. You just do something like:

File.open('test.bin', 'wb') {|file| BinData::Int32be.new(12345).write(file) }

and you don't need to remember any Array#pack codes.

2 Comments

the gem is doing the packing/unpacking for you. source code here. like the answer says, its just keeps you from having to remember how/which pack codes to use
Thanks for this answer, I believe it is easier and cleaner, even though requires an extra gem.
25

There is File#binwrite to write binary data:

File.binwrite("test.bin", my_binary_data)

1 Comment

Better than the accepted answer and File.open do
3

There are Marshal.dump and Marshal.load methods you can use.

Here's a link: http://en.wikipedia.org/wiki/Serialization#Ruby.

And another that saves the data to a file: http://rubylearning.com/satishtalim/object_serialization.html.

2 Comments

This works ok, but I still couldn't get 4-byte file using this: File.open('test.bin', 'wb') {|file| file << Marshal.dump(12345) }, I'm getting 6-byte file. The problem is that I need to create a binary file of proprietary format, this file consists of records, each of them is fixed-length C-style record with integers, bytes, etc. Thanks for really quick answer though.
No worries. Pesto's answer looks very promising.
-16

In my humble oppinion, ruby wasn't made for such tasks. If you have to write to binary files a lot, it would be easiest to write some c functions for that and call them from ruby, which is quite easy using swig. I'm doing the same thing at the moment to write a raid simulator.

3 Comments

That's right - Ruby was meant for other stuff probably, but we have a pretty big Rails application already and we just faced this need to operate with binary files. Thanks for your answer though - we might consider writing the native extension in C/C++.
The problem with writing those functions in C/C++ is that doing so end up could tying them to a specific Ruby implementation. See stackoverflow.com/questions/3292610/what-version-of-ruby/…
As this question pops up as top google result when searching for "ruby write binary", and this answer is actually showed as relevant blurb directly in the search results: Ruby is perfectly fine for what OP laid out. Writing a C function, pulling in a whole new ecosystem (SWIG/C), possibly getting other devs on speed with C just to write some binary data would be hilariously complex compared to simply using the existing methods to work with binary data (see other answers).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.