* byte swapping to get data readable on a decstations
@ 1994-11-10 13:44 Edward Hartnett
1994-11-10 16:07 ` Steve Lionel
` (5 more replies)
0 siblings, 6 replies; 8+ messages in thread
From: Edward Hartnett @ 1994-11-10 13:44 UTC (permalink / raw)
Howdy all.
We here distribute some floating point (IEEE) data to various
users. We generate it on a cray and various workstations, and now we
have a user who wants to read the data from a fortran program on a
decstation. He complains that he can't do this because of the order
that the bytes are stored in. Is there any hope that we can help him
solve his problem?
--
Edward Hartnett ejh@larry.gsfc.nasa.gov
(301) 286-2396 fax: (301) 286-1754
"Tut! Tut!" cried Sherlock Holmes. "You must act, man, or you are
lost. Nothing but energy can save you. This is no time for dispair."
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: byte swapping to get data readable on a decstations
1994-11-10 13:44 byte swapping to get data readable on a decstations Edward Hartnett
@ 1994-11-10 16:07 ` Steve Lionel
1994-11-10 16:47 ` Chuck Campbell
` (4 subsequent siblings)
5 siblings, 0 replies; 8+ messages in thread
From: Steve Lionel @ 1994-11-10 16:07 UTC (permalink / raw)
In article <EJH.94Nov10084407@larry.gsfc.nasa.gov>, ejh@larry.gsfc.nasa.gov
(Edward Hartnett) writes:
|>We here distribute some floating point (IEEE) data to various
|>users. We generate it on a cray and various workstations, and now we
|>have a user who wants to read the data from a fortran program on a
|>decstation. He complains that he can't do this because of the order
|>that the bytes are stored in. Is there any hope that we can help him
|>solve his problem?
Simplicity itself, if he's using a reasonably current version of DEC Fortran.
Have him add CONVERT='BIG_ENDIAN' to the OPEN statement for the unit
in question - this will cause DEC Fortran to do the byte swapping
during unformatted transfers of scalars and arrays (but not RECORDs)
automatically. An alternative is to define the environment variable
FORT_CONVERTn , where n is the unit number, to BIG_ENDIAN before running
the program.
This feature is available in DEC Fortran for all supported platforms
(RISC ULTRIX, DEC OSF/1 AXP, OpenVMS AXP, OpenVMS VAX, Windows NT AXP).
Details vary on the "environment variable" method - see the User Manual
for further details.
Steve Lionel Mail: lionel@quark.enet.dec.com
DEC Fortran Development WWW: http://www.digital.com/info/slionel.html
Digital Equipment Corporation
110 Spit Brook Road, ZKO2-3/N30
Nashua, NH 03062-2698 "Free advice is worth every cent"
For a summary of Digital's Internet services, send mail to info@digital.com
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: byte swapping to get data readable on a decstations
1994-11-10 13:44 byte swapping to get data readable on a decstations Edward Hartnett
1994-11-10 16:07 ` Steve Lionel
@ 1994-11-10 16:47 ` Chuck Campbell
1994-11-11 9:31 ` Ken Bell
` (3 subsequent siblings)
5 siblings, 0 replies; 8+ messages in thread
From: Chuck Campbell @ 1994-11-10 16:47 UTC (permalink / raw)
Edward Hartnett (ejh@larry.gsfc.nasa.gov) wrote:
: Howdy all.
: We here distribute some floating point (IEEE) data to various
: users. We generate it on a cray and various workstations, and now we
: have a user who wants to read the data from a fortran program on a
: decstation. He complains that he can't do this because of the order
: that the bytes are stored in. Is there any hope that we can help him
: solve his problem?
: --
: Edward Hartnett ejh@larry.gsfc.nasa.gov
: (301) 286-2396 fax: (301) 286-1754
: "Tut! Tut!" cried Sherlock Holmes. "You must act, man, or you are
: lost. Nothing but energy can save you. This is no time for dispair."
He can use a unix command -
dd if=input of=output conv=swab
-chuck
--
ACCEL Services, Inc. | Specialists in Gravity, Magnetics
1980 Post Oak Blvd., Suite 2050 | and Integrated Interpretation
Houston, TX, 77056 |
| 1(713)993-0671 voice
Chuck Campbell | 1(713)960-1157 fax
President & Senior Geoscientist | email campbell@neosoft.com
"Integration means more than having all the maps at the same scale!"
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: byte swapping to get data readable on a decstations
1994-11-10 13:44 byte swapping to get data readable on a decstations Edward Hartnett
1994-11-10 16:07 ` Steve Lionel
1994-11-10 16:47 ` Chuck Campbell
@ 1994-11-11 9:31 ` Ken Bell
1994-11-14 21:55 ` Eric Frey
1994-11-11 23:18 ` Ken Bell
` (2 subsequent siblings)
5 siblings, 1 reply; 8+ messages in thread
From: Ken Bell @ 1994-11-11 9:31 UTC (permalink / raw)
In article <EJH.94Nov10084407@larry.gsfc.nasa.gov>,
Edward Hartnett <ejh@larry.gsfc.nasa.gov> wrote:
>
>Howdy all.
>
>We here distribute some floating point (IEEE) data to various
>users. We generate it on a cray and various workstations, and now we
>have a user who wants to read the data from a fortran program on a
>decstation. He complains that he can't do this because of the order
>that the bytes are stored in. Is there any hope that we can help him
>solve his problem?
>--
>Edward Hartnett ejh@larry.gsfc.nasa.gov
> (301) 286-2396 fax: (301) 286-1754
>
>"Tut! Tut!" cried Sherlock Holmes. "You must act, man, or you are
>lost. Nothing but energy can save you. This is no time for dispair."
In C, you can use the swab() function to swap bytes ... exchanges
adjacent even and odd bytes in a given array. It's easy enough
to write such a routine in fortran, something like ...
SUBROUTINE SWAB( A, NBYTES )
REAL*4 A(*)
INTEGER NBYTES
C
REAL*4 NOTVAX, VAX
CHAR*1 SWAPIN(4), SWAPOU(4)
EQUIVALENCE (NOTVAX, SWAPIN)
EQUIVALENCE (VAX, SWAPOU)
C
C "NBYTES" must be even
C
IF (MOD(NBYTES,2) .EQ. 0) THEN
N = NBYTES
ELSE
N = NBYTES - 1
ENDIF
C
DO 10 I = 1, N
NOTVAX = A(I)
SWAPOU(1) = SWAPIN(2)
SWAPOU(2) = SWAPIN(1)
SWAPOU(3) = SWAPIN(4)
SWAPOU(4) = SWAPIN(3)
A(I) = VAX
10 CONTINUE
RETURN
END
--
Ken Bell :: kenbell@panix.com :: (212) 475-4976
======== :: syklb@nasagiss.giss.nasa.gov :: (212) 678-5545
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: byte swapping to get data readable on a decstations
1994-11-10 13:44 byte swapping to get data readable on a decstations Edward Hartnett
` (2 preceding siblings ...)
1994-11-11 9:31 ` Ken Bell
@ 1994-11-11 23:18 ` Ken Bell
1994-11-14 14:19 ` Roger Grady
1994-11-16 4:06 ` byte swapping to get data readable on a dec John Lovell
5 siblings, 0 replies; 8+ messages in thread
From: Ken Bell @ 1994-11-11 23:18 UTC (permalink / raw)
In article <EJH.94Nov10084407@larry.gsfc.nasa.gov>,
Edward Hartnett <ejh@larry.gsfc.nasa.gov> wrote:
>
>Howdy all.
>
>We here distribute some floating point (IEEE) data to various
>users. We generate it on a cray and various workstations, and now we
>have a user who wants to read the data from a fortran program on a
>decstation. He complains that he can't do this because of the order
>that the bytes are stored in. Is there any hope that we can help him
>solve his problem?
>--
>Edward Hartnett ejh@larry.gsfc.nasa.gov
> (301) 286-2396 fax: (301) 286-1754
>
>"Tut! Tut!" cried Sherlock Holmes. "You must act, man, or you are
>lost. Nothing but energy can save you. This is no time for dispair."
Assuming that you just want to turn a Little-Endian ieee-real into
a Big-Endian ieee-real (or vice versa), it's easy enough to write
such a routine in fortran, something like ...
INTEGER FUNCTION SWABYT( A, N )
REAL*4 A(*)
INTEGER N
C
REAL*4 RIN, ROUT
CHAR*1 CIN(4), COUT(4)
EQUIVALENCE (RIN, CIN)
EQUIVALENCE (ROUT, COUT)
C
C--- Return 1 on error, 0 on success
C
IF (N .LT. 1) THEN
SWABYT = 1
RETURN
ENDIF
C
DO 10 I = 1, N
RIN = A(I)
COUT(1) = CIN(4)
COUT(2) = CIN(3)
COUT(3) = CIN(2)
COUT(4) = CIN(1)
A(I) = ROUT
10 CONTINUE
SWABYT = 0
RETURN
END
(Or should bytes 1-2 and 3-4 be swapped for VAXen? It shouldn't be
hard to determine by experiment.)
--
Ken Bell :: kenbell@panix.com :: (212) 475-4976
======== :: syklb@nasagiss.giss.nasa.gov :: (212) 678-5545
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: byte swapping to get data readable on a decstations
1994-11-10 13:44 byte swapping to get data readable on a decstations Edward Hartnett
` (3 preceding siblings ...)
1994-11-11 23:18 ` Ken Bell
@ 1994-11-14 14:19 ` Roger Grady
1994-11-16 4:06 ` byte swapping to get data readable on a dec John Lovell
5 siblings, 0 replies; 8+ messages in thread
From: Roger Grady @ 1994-11-14 14:19 UTC (permalink / raw)
In article <EJH.94Nov10084407@larry.gsfc.nasa.gov>, ejh@larry.gsfc.nasa.gov (Edward Hartnett) writes:
>
> Howdy all.
>
> We here distribute some floating point (IEEE) data to various
> users. We generate it on a cray and various workstations, and now we
> have a user who wants to read the data from a fortran program on a
> decstation. He complains that he can't do this because of the order
> that the bytes are stored in. Is there any hope that we can help him
> solve his problem?
I solved a similar problem by reading the data as character, shuffling
the bytes as necessary and ending up with the rearranged bytes in a
character variable Equivalence'd to a real variable. If you'd like
more specific information, E-mail me and I'll send you some code
fragments or try to answer specific questions.
Roger Grady c21rag@kocrsv01.delcoelect.com
Delco Electronics Corp. Kokomo, IN
"All information and opinions are personal unless otherwise stated."
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: byte swapping to get data readable on a decstations
1994-11-11 9:31 ` Ken Bell
@ 1994-11-14 21:55 ` Eric Frey
0 siblings, 0 replies; 8+ messages in thread
From: Eric Frey @ 1994-11-14 21:55 UTC (permalink / raw)
There were several errors in previous responses to this questions. First,
note that the swapping that needs to be done for 4 byte datatypes is not
the same as doing two swaps of the consectutive 2 byte elements. I.e.,
doing 2 swabs won!t work! Nor will using dd with convert=swab. You need
to swap bytes 1 and 4 and 2 and 3, as pointed out in one of the posts. A
stupid C program to accomplish this (one I use all the time) follows. I!m
not sure that this will work for CRAYs, since I think they use (8 byte
floats).
--------
/* swapwords
Descrpiton: this is a simple program for swapping the order of bytes
in a
binary files consiting oe either 4 byte integers or floating point
values.
Usage: swapwords infile outfile
Revision history:
written 10/25/89 eric frey
*/
#include <stdio.h>
#define SWAP(a,b,t) t = a; a = b; b = t
main(argc,argv)
int argc;
char **argv;
{
int count=0;
FILE *fpin, *fpout;
char errbuf[256];
char buf[4];/* buffers whose bytes are to be swapped*/
char tmp;
if (argc != 3){
fprintf(stderr,"usage: swapwords infile outfile\n");
exit(1);
}
if ((fpin = fopen(*++argv,"r")) == NULL){
sprintf(errbuf, "can't open %s for input", *argv);
perror(errbuf);
exit(1);
}
if ((fpout = fopen(*++argv,"w")) == NULL){
sprintf(errbuf, "can't open %s for output", *argv);
perror(errbuf);
}
while(fread(buf,4,1,fpin) == 1){
count++;
SWAP(buf[0], buf[3], tmp);
SWAP(buf[1], buf[2], tmp);
if (fwrite(buf,4,1,fpout) != 1){
perror("error writing output file");
exit(1);
}
}
fprintf(stderr,"swapped %d 4 byte integers\n",count);
return(0);
}
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: byte swapping to get data readable on a dec
1994-11-10 13:44 byte swapping to get data readable on a decstations Edward Hartnett
` (4 preceding siblings ...)
1994-11-14 14:19 ` Roger Grady
@ 1994-11-16 4:06 ` John Lovell
5 siblings, 0 replies; 8+ messages in thread
From: John Lovell @ 1994-11-16 4:06 UTC (permalink / raw)
In article 94Nov10084407@larry.gsfc.nasa.gov, ejh@larry.gsfc.nasa.gov (Edward Hartnett) writes:
>
> We here distribute some floating point (IEEE) data to various
> users. We generate it on a cray and various workstations, and now we
> have a user who wants to read the data from a fortran program on a
> decstation. He complains that he can't do this because of the order
> that the bytes are stored in. Is there any hope that we can help him
> solve his problem?
In the FORTRAN program, open the file as
open(file='file.dat', convert='big_endian', form='unformatted',....)
and any big_endian integer or IEEE floats will be converted automatically
as the files are read in. If all of the unformatted files are big_endian
you can alternatively do this at compile time as
FORTRAN/CONVERT=BIG_ENDIAN
On an AXP, if you compile the Fortran source with the flag /FLOAT=IEEE then
everything will be done in IEEE format.
Regards,
John
p.s. there is also a /CONVERT=CRAY option which is big-endian integer and
"CRAY" floating point for REAL*8 and COMPLEX*16 (with no single precision).
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~1994-11-16 4:06 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1994-11-10 13:44 byte swapping to get data readable on a decstations Edward Hartnett
1994-11-10 16:07 ` Steve Lionel
1994-11-10 16:47 ` Chuck Campbell
1994-11-11 9:31 ` Ken Bell
1994-11-14 21:55 ` Eric Frey
1994-11-11 23:18 ` Ken Bell
1994-11-14 14:19 ` Roger Grady
1994-11-16 4:06 ` byte swapping to get data readable on a dec John Lovell
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox