Re: Converting Byte Arrays [message #10032] |
Tue, 07 October 1997 00:00 |
Michael Slameczka
Messages: 9 Registered: November 1996
|
Junior Member |
|
|
Liam Gumley wrote:
....
> record = swap_endian( record )
^^^^^^^^^^^
where is this function? It is not mentioned in my help-file of PV-Wave!
cheers
michael
|
|
|
Re: Converting Byte Arrays [message #10046 is a reply to message #10032] |
Mon, 06 October 1997 00:00  |
David Foster
Messages: 341 Registered: January 1996
|
Senior Member |
|
|
Thomas Price wrote:
>
> I am struggling with a file i/o problem. I have a data file which comes
> from a PC. I know the data structure in terms of how many bytes correspond
> to each entry in the file and what file type for each of these entries
> (i.e. integer, real, etc). Generally, I two bytes into a variable for a
> integer and 4 bytes for a real. I can convert the integers simply enough.
> For a variable named id which is a bytarr(2) the integer is simply
> 256*id(1)+id(0). However, how can I convert the 4 byte arrays which are
> floats into the proper numbers? I seem to come up with gibberish if I do a
> simple float(val) where val=byytarr(4).
>
> Any thoughts or tricks? All help much appreciated.
Remember that the byte-ordering on Suns and PCs is reversed. I think
the easiest and most efficient thing to do would be to read the
data into variables directly, using the appropriate data-type,
and then convert them using the BYTEORDER() routine.
If you are going to convert short integers by hand then I think
you might need:
256*id[0]+id[1]
to account for the reversed byte ordering.
Dave
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~
David S. Foster Univ. of California, San Diego
Programmer/Analyst Brain Image Analysis Laboratory
foster@bial1.ucsd.edu Department of Psychiatry
(619) 622-5892 8950 Via La Jolla Drive, Suite 2240
La Jolla, CA 92037
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~
|
|
|
Re: Converting Byte Arrays [message #10048 is a reply to message #10046] |
Mon, 06 October 1997 00:00  |
Liam Gumley
Messages: 473 Registered: November 1994
|
Senior Member |
|
|
Thomas Price wrote:
> I am struggling with a file i/o problem. I have a data file which comes
> from a PC. I know the data structure in terms of how many bytes correspond
> to each entry in the file and what file type for each of these entries
> (i.e. integer, real, etc). Generally, I two bytes into a variable for a
> integer and 4 bytes for a real. I can convert the integers simply enough.
> For a variable named id which is a bytarr(2) the integer is simply
> 256*id(1)+id(0). However, how can I convert the 4 byte arrays which are
> floats into the proper numbers? I seem to come up with gibberish if I do a
> simple float(val) where val=byytarr(4).
One way to read a data file with mixed variable types (integers, floats) is by
using an anonymous structure, e.g.
record = { var1:0, var2:0L, var3:0.0 }
openr, lun, file, /get_lun
readu, lun, record
free_lun, lun
help, record.var1, record.var2, record.var3
will read the first three variables from the file, where
var1 is a 16 bit signed integer,
var2 is a 32 bit signed integer,
var3 is a 32 bit float.
If you need to swap bytes to go from PC to Unix, just do
record = swap_endian( record )
help, record.var1, record.var2, record.var3
If you have many similar records to read, then you can use
data = make_array( nrecords, value = record )
to make an array of structures, and then read this array in one hit.
Cheers,
Liam.
|
|
|