From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1164f8,c1a55e8ae06d8e76 X-Google-Attributes: gid1164f8,public From: Dan.Pop@mail.cern.ch (Dan Pop) Subject: Re: Representation of float on DEC-ALPHA Date: 1995/10/16 Message-ID: #1/1 X-Deja-AN: 117629086 sender: news@news.cern.ch (USENET News System) x-nntp-posting-host: ues5.cern.ch references: <45thnl$klu@galadriel.iwb.mw.tu-muenchen.de> organization: CERN European Lab for Particle Physics newsgroups: comp.sys.dec 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 > >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