Reading a 64-but double in a file

R

Ralph Shnelvar

I have a file I need to read.

The file has ANSI text interspersed with doubles (64-bit standard floating point numbers).

Let's say the floating point number is at 9 bytes from the beginning of the file. How can I read the number and then convert the number to an integer?

I scoured Dave Thomas' "Programming Ruby 1.9" and the web but could find nothing about this.

It's gotta be easy, doesn't it?
 
J

Jeremy Bopp

I have a file I need to read.

The file has ANSI text interspersed with doubles (64-bit standard floating point numbers).

Let's say the floating point number is at 9 bytes from the beginning of the file. How can I read the number and then convert the number to an integer?

I scoured Dave Thomas' "Programming Ruby 1.9" and the web but could find nothing about this.

It's gotta be easy, doesn't it?

Assuming you're running with a 64-bit enabled Ruby and the bytes in the
file are in network byte order:

# Open the file for reading with a binary encoding.
File.open('datafile', 'rb') do |f|
# Seek to the first byte of the floating point data.
f.seek(8)
# Read 8 bytes of data to get the 64-bit float.
the_float_as_string = f.read(8)
# Convert the string of bytes into a floating point number.
the_float = the_float_as_string.unpack('G')[0]
# Convert the floating point number to an integer.
the_int = the_float.to_i

puts the_int
end

It's possible that this may still work on a 32-bit Ruby, but I don't
have one to test with right now. The issue would be the behavior of the
'G' format directive for the unpack method. It may only handle 32-bit
floats on 32-bit Ruby interpreters. I'm not sure.

See the documentation for String#unpack for more directives in case your
file uses something other than network byte order to store the floats.

-Jeremy
 
R

Ralph Shnelvar

Saturday, March 19, 2011, 9:10:25 PM, you wrote:


JB> Assuming you're running with a 64-bit enabled Ruby and the bytes in the
JB> file are in network byte order:

JB> # Open the file for reading with a binary encoding.
JB> File.open('datafile', 'rb') do |f|
JB> # Seek to the first byte of the floating point data.
JB> f.seek(8)
JB> # Read 8 bytes of data to get the 64-bit float.
JB> the_float_as_string = f.read(8)
JB> # Convert the string of bytes into a floating point number.
JB> the_float = the_float_as_string.unpack('G')[0]
JB> # Convert the floating point number to an integer.
JB> the_int = the_float.to_i

JB> puts the_int
JB> end

JB> It's possible that this may still work on a 32-bit Ruby, but I don't
JB> have one to test with right now. The issue would be the behavior of the
JB> 'G' format directive for the unpack method. It may only handle 32-bit
JB> floats on 32-bit Ruby interpreters. I'm not sure.

JB> See the documentation for String#unpack for more directives in case your
JB> file uses something other than network byte order to store the floats.

JB> -Jeremy

May the gods bless you, Jeremy. Exactly the answer I was looking for.
 

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
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top