Date: Thu, 24 Feb 2000 12:26:31 +0200 (IST) From: Eli Zaretskii X-Sender: eliz AT is To: Dieter Buerssner cc: djgpp AT delorie DOT com Subject: Re: binary to float In-Reply-To: <890r5l$1qjno$1@fu-berlin.de> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII 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 Precedence: bulk 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.