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.

···

--
Posted via http://www.ruby-forum.com/.

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

···

On 6/15/07, Jim flip <james@dimsum.tv> wrote:

--
Posted via http://www.ruby-forum.com/\.

--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

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.

···

--
Alex

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 Eden

···

On 6/15/07, Jim flip <james@dimsum.tv> 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.

--
Posted via http://www.ruby-forum.com/\.

--
Cell: 808 782-5046
Current Location: Melbourne, FL

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

Cheers,
Jim.

···

--
Posted via http://www.ruby-forum.com/.

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:

···

--
Posted via http://www.ruby-forum.com/.

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.

···

On 6/15/07, John Joyce <dangerwillrobinsondanger@gmail.com> wrote:

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

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.

···

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

On 6/15/07, John Joyce <dangerwillrobinsondanger@gmail.com> wrote: