[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

wite an integer to file as binary

Jim flip

6/15/2007 11:25:00 AM

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-....

8 Answers

Robert Dober

6/15/2007 11:36:00 AM

0

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.
>

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

> --
> Posted via http://www.ruby-....
>
>


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

Alex Young

6/15/2007 11:39:00 AM

0

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

Jim flip

6/15/2007 11:43:00 AM

0

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-....

Anthony Eden

6/15/2007 12:14:00 PM

0

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

http://bindata.ruby...

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-....
>
>


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

John Joyce

6/15/2007 4:48:00 PM

0

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

Rick DeNatale

6/17/2007 7:36:00 PM

0

On 6/15/07, John Joyce <dangerwillrobinsondanger@gmail.com> 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.denh...

John Joyce

6/17/2007 8:02:00 PM

0


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

> On 6/15/07, John Joyce <dangerwillrobinsondanger@gmail.com> 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.

Jim flip

6/18/2007 10:49:00 AM

0

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

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