[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

20000511: Converting DEC Alpha double to HP



------- Forwarded Message

Date: Thu, 11 May 2000 16:41:42 GMT
Organization: NASA/JSFC
To: address@hidden
Subject: Re: Converting DEC Alpha double to HP

Tim Oram <address@hidden> writes:

>> I'm trying read a structure from a file created on a DEC alpha.
>> Unfortunately, one of the structure's values is stored in the file
>> in the Dec Alpha double internal representation.  (I hope the
>> question is clear since I'm not positive I'm using the appropriate
>> terminology).
>> 
>> Does anyone have a routine to perform the appropriate manipulations
>> to convert this to something usable on an HP?
>> 
>> Thanks for any help.

All you have to do is reverse the byte order.  How to do this depends
on the computer language you are using, but in C:

/* Call with the ADDRESS of the value to swap */
double swapdouble(void *value)
{
   union {
      double d;
      char c[8];
   } in, out;

/* Use memcpy to avoid possible floating point exception */
   memcpy(&in.d, value, sizeof(in.d));
   out.c[0] = in.c[7];
   out.c[1] = in.c[6];
   out.c[2] = in.c[5];
   out.c[3] = in.c[4];
   out.c[4] = in.c[3];
   out.c[5] = in.c[2];
   out.c[6] = in.c[1];
   out.c[7] = in.c[0];
   return (out.d);
}

- -- 
+------------------------------+--------------------------------------+
| Daniel K. Forrest            | Space Science and Engineering Center |
| address@hidden | University of Wisconsin              |
| (608)262-1905                | Madison, WI  53706                   |
+------------------------------+--------------------------------------+

------- End of Forwarded Message

>From: Kirk Swanson <address@hidden>
>Organization: AEROMET
>Date: Thu, 11 May 2000 12:06:25 -0500
>To: address@hidden
>Subject: Re: Converting DEC Alpha double to HP

>Even better (if you have it available) just use the stdlib function "swab"
>which swaps bytes.

>SWAB(3C)
>SWAB(3C)
>
>NAME
>     swab - swap bytes
>
>SYNOPSIS
>     #include <stdlib.h>
>
>     void swab(char *from, char *to, int nbytes);
>
>Kirk

>From: Dan Forrest <address@hidden>
>Date: Thu, 11 May 2000 17:14:38 GMT
>To: address@hidden
>Subject: Re: Converting DEC Alpha double to HP

>Kirk Swanson <address@hidden> writes:

>>> Even better (if you have it available) just use the stdlib function
>>> "swab" which swaps bytes.
>>> 
>>> SWAB(3C)
>>> SWAB(3C)
>>> 
>>> NAME
>>>      swab - swap bytes
>>> 
>>> SYNOPSIS
>>>      #include <stdlib.h>
>>> 
>>>      void swab(char *from, char *to, int nbytes);
>
>This won't work.  "swab" swaps each pair of bytes, i.e. 1/2, 3/4, 5/6,
>and 7/8, but what you want is 1/8, 2/7, 3/6, and 4/5.
>
>-- 
>+------------------------------+--------------------------------------+
>| Daniel K. Forrest            | Space Science and Engineering Center |
>| address@hidden | University of Wisconsin              |
>| (608)262-1905                | Madison, WI  53706                   |
>+------------------------------+--------------------------------------+