# Wite an integer to file as binary

**URL:** <https://www.ruby-forum.com/t/wite-an-integer-to-file-as-binary/106928>\
**Category:** Ruby\
**Created:** [June 15, 2007, 1:24pm UTC](https://www.ruby-forum.com/t/wite-an-integer-to-file-as-binary/106928 "2007-06-15T13:24:32Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [June 15, 2007, 1:24pm UTC](https://www.ruby-forum.com/t/wite-an-integer-to-file-as-binary/106928/1 "2007-06-15T13:24:32Z")

</div>

Simple problem but I’m not sure of an elegant solution, I can do it but  
all a bit minging.

Basically have a numeric value and want to write the value as a 4 byte  
integer to a binary file.

Thanks for any insight,  
Jim.

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [June 15, 2007, 1:36pm UTC](https://www.ruby-forum.com/t/wite-an-integer-to-file-as-binary/106928/2 "2007-06-15T13:36:40Z")

</div>

On 6/15/07, Jim flip [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Simple problem but I’m not sure of an elegant solution, I can do it but  
> all a bit minging.
> 
> Basically have a numeric value and want to write the value as a 4 byte  
> integer to a binary file.
> 
> Thanks for any insight,  
> Jim.

A first idea - not tested - would be something like this  
class IO  
def write\_int\_4 int  
4.times do  
putc int=int/256  
end  
end  
end

You gotta play around with alignment I am afraid, but the basics are  
there

HTH  
Robert

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [June 15, 2007, 1:40pm UTC](https://www.ruby-forum.com/t/wite-an-integer-to-file-as-binary/106928/3 "2007-06-15T13:40:41Z")

</div>

Jim flip wrote:

> Simple problem but I’m not sure of an elegant solution, I can do it but  
> all a bit minging.
> 
> Basically have a numeric value and want to write the value as a 4 byte  
> integer to a binary file.
> 
> Thanks for any insight,  
> Jim.

File.open(‘output.bin’, ‘wb’){|f|  
f.write [val].pack(‘N’)  
}

Should do it, if you want network byte order. ri Array#pack if you want  
more info.

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [June 15, 2007, 1:43pm UTC](https://www.ruby-forum.com/t/wite-an-integer-to-file-as-binary/106928/4 "2007-06-15T13:43:13Z")

</div>

Thanks, thought the only way was to but it into an array, my syntax  
wasn’t as neat though.

Cheers,  
Jim.

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [June 15, 2007, 2:14pm UTC](https://www.ruby-forum.com/t/wite-an-integer-to-file-as-binary/106928/5 "2007-06-15T14:14:47Z")

</div>

Take a look at the bindata gem for all your binary needs. I’ve used it  
and it is quite slick.

[http://bindata.rubyforge.org/](http://bindata.rubyforge.org/)

V/r  
Anthony E.

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [June 17, 2007, 9:37pm UTC](https://www.ruby-forum.com/t/wite-an-integer-to-file-as-binary/106928/6 "2007-06-17T21:37:04Z")

</div>

On 6/15/07, John J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This one is always a stumper.  
> use the method to\_s(2)  
> 5.to\_s(2)  
> Converts 5 to base 2 (binary) as a string.  
> This is fine for output to a file!

Except that the OP was looking to write the integer as 4 binary bytes.  
Array#pack is the way to go.

> Alternatively, if you’re looking to set binary write mode for Windows  
> (versus text mode. this is windows only)  
> there is binmode

That doesn’t afffect how data is written, it keeps windows from  
processing control characters and doing things like prematurely giving  
an end-of-file if the data contains a ctrl-D character.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [June 17, 2007, 10:03pm UTC](https://www.ruby-forum.com/t/wite-an-integer-to-file-as-binary/106928/7 "2007-06-17T22:03:08Z")

</div>

On Jun 17, 2007, at 2:36 PM, Rick DeNatale wrote:

> On 6/15/07, John J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > This one is always a stumper.  
> > use the method to\_s(2)  
> > 5.to\_s(2)  
> > Converts 5 to base 2 (binary) as a string.  
> > This is fine for output to a file!
> 
> Except that the OP was looking to write the integer as 4 binary bytes.  
> Array#pack is the way to go.

Oops! I missed the part about 4 binary bytes. Sometimes the inbox is  
overflowing and read too fast…

> > Alternatively, if you’re looking to set binary write mode for Windows  
> > (versus text mode. this is windows only)  
> > there is binmode
> 
> That doesn’t afffect how data is written, it keeps windows from  
> processing control characters and doing things like prematurely giving  
> an end-of-file if the data contains a ctrl-D character.

Someday, Windows will be another unix clone.

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [June 15, 2007, 6:49pm UTC](https://www.ruby-forum.com/t/wite-an-integer-to-file-as-binary/106928/8 "2007-06-15T18:49:32Z")

</div>

This one is always a stumper.  
use the method to\_s(2)  
5.to\_s(2)  
Converts 5 to base 2 (binary) as a string.  
This is fine for output to a file!

Alternatively, if you’re looking to set binary write mode for Windows  
(versus text mode. this is windows only)  
there is binmode

---

<div class="post-metadata">

**Author:** ![jimb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f4b2a3/32.png) [@jimb](https://www.ruby-forum.com/u/jimb)\
**Post date:** [June 18, 2007, 12:49pm UTC](https://www.ruby-forum.com/t/wite-an-integer-to-file-as-binary/106928/9 "2007-06-18T12:49:25Z")

</div>

Gahhh wish I’d heard of bindata gem a few months ago, now I feel like  
going back and rewriting loads of code ☹
