delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/02/25/09:44:33

From: pavenis AT lanet DOT lv
Message-ID: <B0000119469@stargate.astr.lu.lv>
To: djgpp AT delorie DOT com, Dieter Buerssner <buers AT gmx DOT de>
Date: Fri, 25 Feb 2000 16:14:56 +0200
MIME-Version: 1.0
Subject: Re: binary to float
References: <200002241539 DOT RAA16262 AT is DOT elta DOT co DOT il>
In-reply-to: <Pine.SUN.3.91.1000224184303.16581A-100000@is>
X-mailer: Pegasus Mail for Win32 (v3.12b)
Reply-To: djgpp AT delorie DOT com

On 24 Feb 00, at 18:45, Eli Zaretskii wrote:

> 
> On Thu, 24 Feb 2000, Dieter Buerssner wrote:
> 
> > unsigned long l;
> > float x;
> > fread(&l, sizeof l, 1, fp);
> > l = ntohl(l);
> > x = *(float *)&l;
> > 
> > My question, is this really more portable? In the libc info for ntohl 
> > under Portability I read: not ANSI, not POSIX. Is this function 
> > generally available?
> 

This my not work with -fstrict-aliasing (default for gcc-2.95, gcc-2.95.1, 
and perhaps future released of gcc, but not gcc-2.95.2). In this case I 
suggest to use union

union { long l; float x; } X;
fread(&X.l sizeof(X.l), 1, fp);
X.l = ntohl(X.l);

An exception: pointer to char (or unsigned char) can alias anything

Andris

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019