From: Mike <mike@alpha.nmrlab.hscsyr.edu>
To: axp-list@redhat.com
Subject: Re: byte order, swapping code
Date: Tue, 16 Jun 1998 09:54:27 -0400 [thread overview]
Message-ID: <199806161354.JAA05199@alpha.nmrlab.hscsyr.edu> (raw)
If you don't mind creating a new data file with the swapped bytes
it is pretty easy. Just read in the file as char's and write them out
in reverse order in sets of 2's, 4's, or 8's for 16 bit, 32 bit, and 64 bit
data. But if the files are large and you frequently move back and forth
between architectures (like we do here, our Sun and Alpha both access the
same hard disk) you might want to do the conversion on the fly.
The following is a function that will read a file of 32 bit floats
in either big or little endian format. Our programs detect what architecture
they are running on and pass that info to the function as a flag.
size in this case is sizeof(float).
void fread_float(float *data, int size, int nitems, FILE *stream,int arch)
{ /* arch 0=don't swap, 1=we need to swap */
union { char cval[4]; float fval; } sw;
int i;
char temp;
if (arch)
for (i=0;i<nitems;i++) {
fread(sw.cval,size,1,stream);
temp=sw.cval[0]; sw.cval[0]=sw.cval[3];
sw.cval[3]=temp; temp=sw.cval[1];
sw.cval[1]=sw.cval[2]; sw.cval[2]=temp;
data[i]=sw.fval;
}
else
fread(data,size,nitems,stream);
}
Hope this helps.
Michael
next reply other threads:[~1998-06-16 13:54 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
1998-06-16 13:54 Mike [this message]
1998-06-16 14:40 ` byte order, swapping code Paul Tomblin
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox