Usenet comp.sys.dec (1990-2013)
 help / color / mirror / Atom feed
From: Dan.Pop@mail.cern.ch (Dan Pop)
Subject: Re: Representation of float on DEC-ALPHA
Date: 1995/10/16	[thread overview]
Message-ID: <danpop.813882352@rscernix> (raw)
In-Reply-To: 45thnl$klu@galadriel.iwb.mw.tu-muenchen.de

In <45thnl$klu@galadriel.iwb.mw.tu-muenchen.de> rossgod@eowyn.iwb.mw.tu-muenchen.de (Ulrich Rossgoderer) writes:

>I've to read a binary file generated on a SGI or a HP workstation. The
>binary contains float numbers (single precision)?
>
>How do I read (convert) them on a DEC-ALPHA workstation? It can't be
>only done by byte swapping, because the following program

It _can_ be done only by byte swapping.

>------------------8<------------------8<------------------8<---------
>#include <stdio.h>
>
>int main (void)
>{
>   char  *cptr, abbruch;
>   float  f;
>
>   abbruch = 0;
>   do
>   {
>      printf ("Please enter float number: ");
>      if (scanf  ("%f", &f) == 1)
>      {
>         cptr = (char*)&f;
>         printf ("Byte 0: %4d, Byte 1: %4d, Byte 2: %4d, Byte 4: %4d\n",
>                 (int)cptr[0], (int)cptr[1], (int)cptr[2], (int)cptr[3]);
>      }
>      else
>         abbruch = 1;
>
>   } while (!abbruch);
>
>   return (0);
>}
>------------------8<------------------8<------------------8<---------

Classical programming error!  Replace "char" by "unsigned char" in your
program and you'll get the same results, except for the byte order, of
course, on both SGI and Alpha.

On SGI, char is unsigned, while on Alpha, char is signed (the "signedness"
of char is undefined by the language).  

All the workstations (I know of) use IEEE-754 floating point.  Only the
byte sex is different between little endian and big endian platforms.

BTW, you don't have to cast the char arguments of printf.  The compiler
will do it for you, because printf is a variadic function and the 
default argument promotions are performed automatically on all the
arguments following the format string.

Dan
--
Dan Pop
CERN, CN Division
Email: Dan.Pop@mail.cern.ch 
Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



       reply	other threads:[~1995-10-16  4:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <45thnl$klu@galadriel.iwb.mw.tu-muenchen.de>
1995-10-16  4:00 ` Dan Pop [this message]
1995-10-16  4:00   ` Representation of float on DEC-ALPHA Ron Natalie
1995-10-18  4:00     ` Dan Pop
1995-10-20  4:00       ` Phillip Helbig
1995-10-20  4:00         ` Ron Natalie
1995-10-17  4:00   ` Ulrich Rossgoderer
1995-10-17  4:00 ` Steve Lionel
1995-10-16  4:00 Ulrich Rossgoderer
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