[comp.sys.mac.misc] Import Binary Date File from PC

fang@physics.phy.duke.edu (Fang Zhong) (02/06/91)

	I use a PC to automate my experiment. I store my data on the PC in
binary format as:


>	fp = fopen(filename, "ab"); 
>		fwrite(&data, sizeof(float), 1,fp); 
>	fclose(fp);

I can use:

>	fread(&data, sizeof(float), 1, fp);

to read the data back on PC.  I plot my data with Igor on Mac. However, if
I use the same line

>	fread(&data, sizeof(float), 1, fp);

to read the data on Mac after the data has been imported via "default
translation" of Apple File Exchange, I get garbage.  The same is true when
I generate binary data on Mac and read it back on PC.  This problem exits
regardless which mode I use in AFE.

	Can some experts on the network point out what I didn't right?
	Thanks in advance.




--
	Fang Zhong				1-919-684-8247
	Duke University Dept. of Physics	fang@phy.duke.edu
	Durham, N.C.      27706			

wishart@bnl.gov (Jim Wishart) (02/13/91)

In article <21958@duke.cs.duke.edu> fang@physics.phy.duke.edu (Fang Zhong) 
writes:
>         I use a PC to automate my experiment. I store my data on the PC
> in binary format as:  fwrite(&data, sizeof(float), 1,fp); 
> I can use:  fread(&data, sizeof(float), 1, fp);
> to read the data back on PC.  I plot my data with Igor on Mac. However, 
> if I use the same line       (deleted)
> to read the data on Mac after the data has been imported via "default
> translation" of Apple File Exchange, I get garbage.  The same is true
> when I generate binary data on Mac and read it back on PC.  This problem
 > exits regardless which mode I use in AFE.

The problem is that the byte order is opposite on Intel and Motorola 
microprocessors.  On Intel machines the order is least significant to most 
significant byte, while Motorola is MSB->LSB.

You must write your own translation routines to swap the bytes between 
formats.  I did this in Fortran for a program ported from MS-DOS so that I 
could maintain file compatibility.  I was reading mixed groups of reals, 
short integers and strings in large blocks and swapping the bytes in 
memory.  When it came time to save, the bytes were swapped back to write 
in Intel format.  I wrote one routine to swap bytes and another to swap 
words and applied them as necessary.  Strings were not swapped, shorts 
were byte swapped, and longs were word and byte swapped.


Jim Wishart                wishart@bnl.gov
Chemistry Department
Brookhaven National Laboratory,Upton, NY 11973