Converting Byte Arrays [message #10049] |
Mon, 06 October 1997 00:00  |
popprice
Messages: 3 Registered: September 1995
|
Junior Member |
|
|
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.
|
|
|
|
Re: Converting Byte Arrays [message #10167 is a reply to message #10049] |
Thu, 09 October 1997 00:00  |
popprice
Messages: 3 Registered: September 1995
|
Junior Member |
|
|
Just a note. Tried the suggestions and the following worked best.
For integer data the 'BYTEORDER,var' worked perfectly. However, for float
you need 'swap_endian(var)'.
Thanks for the suggestions.
|
|
|
Re: Converting Byte Arrays [message #10172 is a reply to message #10049] |
Wed, 08 October 1997 00:00  |
David Foster
Messages: 341 Registered: January 1996
|
Senior Member |
|
|
Michael Slameczka wrote:
>
> Liam Gumley wrote:
> ....
>> record = swap_endian( record )
> ^^^^^^^^^^^
>
> where is this function? It is not mentioned in my help-file of PV-Wave!
>
> cheers
> michael
It's an IDL function. But perhaps you have BYTEORDER().
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 #10176 is a reply to message #10049] |
Wed, 08 October 1997 00:00  |
davis
Messages: 15 Registered: March 1995
|
Junior Member |
|
|
On Mon, 06 Oct 1997 16:54:55 -0500, Liam Gumley <liam.gumley@ssec.wisc.edu>
wrote:
> 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
This is not really portable because you need to know how the structure
and data are padded. For example, consider the C code:
#include <stdio.h>
typedef struct
{
short s;
long l;
}
Short_Long_Type;
int main (int argc, char **argv)
{
Short_Long_Type sl;
FILE *fp;
sl.s = 1234;
sl.l = 2147483647;
fp = fopen ("sl.dat", "wb");
fwrite ((char *) &sl, sizeof (Short_Long_Type), 1, fp);
fclose (fp);
return 0;
}
In general, the SIZE of data file that this program produces will
vary. Under Linux, it produces an 8 byte file, whereas under DOS the
resulting file is 6 bytes. If you want portable datafiles, never
directly write out the structures.
--John
|
|
|