From: Masahiro Tanaka Date: 2000-12-13T06:17:56+09:00 Subject: [ruby-talk:7109] Re: Reading lots of binary data into arrays >From: matz@zetabits.com (Yukihiro Matsumoto) >Subject: [ruby-talk:7067] Re: Reading lots of binary data into arrays > In message "[ruby-talk:7049] Reading lots of binary data into arrays" > on 00/12/12, Dwight Tuinstra writes: > > |1) Are there packages/extensions/modules available > | for reading large amounts of binary data into arrays > | of structures? I know that binary data can be read > | a "char" at a time, but this is likely to be too > | slow. > > Read fixed sized string using IO#read then unpack it into integers > using String#unpack. For example: > while str = input.read(size_of_an_entry) > ary.push(str.unpack("NNNnn")) # assuming network byte order > end If you need more speed, NArray extension and a script below may help you. Get NArray from: http://www.ruby-lang.org/en/raa-list.rhtml?name=NArray This script uses PGPLOT extension for plotting. Also you can get it from NArray's page. Masa Tanaka -- require 'narray' class MyStruct attr_reader :size def initialize(arg) case arg when String raise ArgumentError, "wrong data size" if arg.size%16 != 0 @size = arg.size/16 @data = NArray.to_na( arg, NArray::BYTE, 16, @size ) else raise ArgumentError, "not String" end end def tag0 @data[ 0..3 ,true].to_type_as_binary(NArray::LINT) end def tag1 @data[ 4..7 ,true].to_type_as_binary(NArray::LINT) end def tag2 @data[ 8..11,true].to_type_as_binary(NArray::LINT) end def tag3 @data[12..13,true].to_type_as_binary(NArray::SINT) end def tag4 @data[14..15,true].to_type_as_binary(NArray::SINT) end end if __FILE__ == $0 fname = $*.shift data = MyStruct.new( open(fname,'r').read ) if require 'pgplot' include Pgplot x = NArray.sfloat(data.size).indgen! pgbeg '/xwin' pgsci 1 pgenv 0, data.size, -300, 300 pglab "x", "y", "Example" pgsci 2 pgline x, data.tag0 pgsci 3 pgline x, data.tag1 pgsci 4 pgline x, data.tag2 pgsci 5 pgline x, data.tag3 pgsci 6 pgline x, data.tag4 end end