* Re: byte order, swapping code
@ 1998-06-16 13:54 Mike
1998-06-16 14:40 ` Paul Tomblin
0 siblings, 1 reply; 2+ messages in thread
From: Mike @ 1998-06-16 13:54 UTC (permalink / raw)
To: axp-list
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
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: byte order, swapping code
1998-06-16 13:54 byte order, swapping code Mike
@ 1998-06-16 14:40 ` Paul Tomblin
0 siblings, 0 replies; 2+ messages in thread
From: Paul Tomblin @ 1998-06-16 14:40 UTC (permalink / raw)
To: axp-list
Quoting Mike (mike@alpha.nmrlab.hscsyr.edu):
> 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.
It's been a long, long, long time since I've done this, but I seem to recall
some functions that came with RPC and NFS that did this sort of thing - it
converted bytes too and from "Network Byte Order" (which just happened to be
the native format of MC68000 and SPARC chips, since it was a Sun protocol).
Since Linux has NFS and RPC, it probably has the libraries. I think it's
either called "xdev" or "rdev".
--
Paul Tomblin, ptomblin@xcski.com.
"The day Microsoft makes something that doesn't suck is probably the day they
start making vacuum cleaners" - Ernst Jan Plugge
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~1998-06-16 14:39 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1998-06-16 13:54 byte order, swapping code Mike
1998-06-16 14:40 ` Paul Tomblin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox