delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/02/24/06:38:17

Date: Thu, 24 Feb 2000 12:26:31 +0200 (IST)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
X-Sender: eliz AT is
To: Dieter Buerssner <buers AT gmx DOT de>
cc: djgpp AT delorie DOT com
Subject: Re: binary to float
In-Reply-To: <890r5l$1qjno$1@fu-berlin.de>
Message-ID: <Pine.SUN.3.91.1000224122613.13848S-100000@is>
MIME-Version: 1.0
Reply-To: djgpp AT delorie DOT com
Errors-To: dj-admin AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

On 23 Feb 2000, Dieter Buerssner wrote:

> The only problem may arise from the endianess of the format.
> If you have to swap endianess, the following code snippet may
> work. (untested)
> 
>   FILE *fp; 
>   unsigned char rawdata[4];
>   unsigned char t;
>   float x;
> 
>   fp = fopen("binfloat.dat", "rb");
>   if (fp) 
>   {
>     if (fread(rawdata, sizeof rawdata, 1, fp) == 1)
>     {
>       /* swap endianess */
>       t = rawdata[0]; rawdata[0] = rawdata[3]; rawdata[3] = t;
>       t = rawdata[1]; rawdata[1] = rawdata[2]; rawdata[2] = t;
>       x = *(float *)rawdata; /* x is the float you wanted */
>     }
>   }

There are library functions (htons, ntohl, etc.) to do this more
portably.

- Raw text -


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