wite an integer to file as binary

J

Jim flip

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

Robert Dober

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
 
A

Alex Young

Jim said:
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.
 
J

Jim flip

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

Cheers,
Jim.
 
J

John Joyce

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
 
R

Rick DeNatale

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

John Joyce

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

Jim flip

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top