From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM, MAILING_LIST_MULTI autolearn=unavailable autolearn_force=no version=3.4.4 X-Google-Thread: 103376,8200c5d9633351c4 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!news4.google.com!news3.google.com!news.glorb.com!newsgate.cistron.nl!newsfeed.stueberl.de!proxad.net!usenet-fr.net!news.enst.fr!melchior!cuivre.fr.eu.org!melchior.frmug.org!not-for-mail From: Marius Amado Alves Newsgroups: comp.lang.ada Subject: Re: How to byte swap an IEEE Float? Date: Tue, 28 Jun 2005 17:59:27 +0100 Organization: Cuivre, Argent, Or Message-ID: References: <1119966822.201891.303810@g49g2000cwa.googlegroups.com> NNTP-Posting-Host: lovelace.ada-france.org Mime-Version: 1.0 (Apple Message framework v622) Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable X-Trace: melchior.cuivre.fr.eu.org 1119978019 80166 212.85.156.195 (28 Jun 2005 17:00:19 GMT) X-Complaints-To: usenet@melchior.cuivre.fr.eu.org NNTP-Posting-Date: Tue, 28 Jun 2005 17:00:19 +0000 (UTC) To: comp.lang.ada@ada-france.org Return-Path: In-Reply-To: X-Mailer: Apple Mail (2.622) X-Virus-Scanned: by amavisd-new at dcc.fc.up.pt X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at ada-france.org X-BeenThere: comp.lang.ada@ada-france.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Gateway to the comp.lang.ada Usenet newsgroup" List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Xref: g2news1.google.com comp.lang.ada:11697 Date: 2005-06-28T17:59:27+01:00 On 28 Jun 2005, at 16:55, Simon Wright wrote: > Marius Amado Alves writes: > >> On 28 Jun 2005, at 14:53, Bj=F6rn wrote: >> >>> I need to read some float values from file that have been written in >>> big-endian byte order from a c-program. The simple swapping = procedure >>> that I have just interchanges the byte order of type IEEE_Float_32 = to >>> get little-endian. The problem is that for some values (eg.=20 >>> 33.229000) >>> it is a "NaN" when doing IEEE_Float_32'Read and I get a constraint >>> error (invalid data) from stream_io when the value is read. How do I >>> get around this? >> >> Simple. Setup an array of bytes with the same representation of the >> float type (use representation clauses and pragma Pack). Read the >> array of bytes. Swap the bytes. Convert to the float type (use >> unchecked conversion). > > Marius, > > I think you mean 'of the same length as the float type'? > > OP doesn't say what processor he gets the BE value from, but assuming > PowerPC you either get Float (4 bytes) for digits <=3D 6 or Long_Float > (8 bytes) for digits 7 .. 15. > > On Intel hardware there's an 80-bit Long_Long_Float (digits 18). Sure. You know, I meant representation =3D bits and bytes, implying same=20= length. It seems the OP already has the float type (IEEE_Float_32),=20 32-bit =3D 4-byte long, and the data are floats of this length, only=20 different in byte order. OP, if the data are of a different length, then you have to unchecked=20 convert first to a float type of that length, and then normal convert=20 to the final type (risking Constraint_Error).