Reading and writing binary data from files

I just started reading about Ruby, and decided to try to use it to write
a utility I need. One of the things that I need to do is to read a file
that contains binary data (32-bit integers, and strings consisting of a
16-bit integer count followed by that many 8-bit chars) created from a C
program. Poking around, I saw the sysread and syswrite calls, which
appear to let me read 4 bytes (for a 32-bit integer, for example) into a
string. The question is, short of picking each byte off one-by-one and
doing the shift-add thing to build up an integer, can I just say
"reinterpret this memory as an integer"? I also need to go the other
way, writing out binary 32-bit integers.

Thanks!
Jeff

Posted too soon! Just found pack and unpack under Array (not exactly
where I was looking for I/O stuff!)

Jeff de Vries wrote:

···

I just started reading about Ruby, and decided to try to use it to
write a utility I need. One of the things that I need to do is to
read a file that contains binary data (32-bit integers, and strings
consisting of a 16-bit integer count followed by that many 8-bit
chars) created from a C program. Poking around, I saw the sysread and
syswrite calls, which appear to let me read 4 bytes (for a 32-bit
integer, for example) into a string. The question is, short of
picking each byte off one-by-one and doing the shift-add thing to
build up an integer, can I just say “reinterpret this memory as an
integer”? I also need to go the other way, writing out binary 32-bit
integers.

Thanks!
Jeff

Jeff de Vries wrote:

Posted too soon! Just found pack and unpack under Array (not exactly
where I was looking for I/O stuff!)

ok

jf

···

Jeff de Vries wrote:

I just started reading about Ruby, and decided to try to use it to
write a utility I need. One of the things that I need to do is to
read a file that contains binary data (32-bit integers, and strings
consisting of a 16-bit integer count followed by that many 8-bit
chars) created from a C program. Poking around, I saw the sysread and
syswrite calls, which appear to let me read 4 bytes (for a 32-bit
integer, for example) into a string. The question is, short of
picking each byte off one-by-one and doing the shift-add thing to
build up an integer, can I just say “reinterpret this memory as an
integer”? I also need to go the other way, writing out binary 32-bit
integers.

Thanks!
Jeff