From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (qmail 12778 invoked from network); 16 Jun 1998 13:54:59 -0000 Received: from alpha.nmrlab.hscsyr.edu (139.127.65.86) by mail2.redhat.com with SMTP; 16 Jun 1998 13:54:59 -0000 Received: (from mike@localhost) by alpha.nmrlab.hscsyr.edu (8.8.7/8.8.7) id JAA05199 for axp-list@redhat.com; Tue, 16 Jun 1998 09:54:27 -0400 Date: Tue, 16 Jun 1998 09:54:27 -0400 From: Mike Message-Id: <199806161354.JAA05199@alpha.nmrlab.hscsyr.edu> To: axp-list@redhat.com Subject: Re: byte order, swapping code 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