Wite an integer to file as binary

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.

On 6/15/07, Jim flip [email protected] 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

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.

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

Cheers,
Jim.

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/

V/r
Anthony E.

On 6/15/07, John J. [email protected] 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/

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

On 6/15/07, John J. [email protected] 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.

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs