delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/07/30/12:33:16

From: "Ian Miller" <Ian AT shelob DOT force9 DOT co DOT uk>
Newsgroups: comp.os.msdos.djgpp
References: <7nku8s$hkl$1 AT nnrp1 DOT deja DOT com>
Subject: Re: Forming a float from 4 seperate bytes
Lines: 32
X-Newsreader: Microsoft Outlook Express 5.00.2014.211
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2014.211
Message-ID: <sepn3.1415$lp.28209@stones>
Date: Tue, 27 Jul 1999 21:34:17 +0100
NNTP-Posting-Host: 212.56.94.148
X-Complaints-To: abuse AT plus DOT net DOT uk
X-Trace: stones 933109784 212.56.94.148 (Tue, 27 Jul 1999 22:09:44 BST)
NNTP-Posting-Date: Tue, 27 Jul 1999 22:09:44 BST
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

> ...4 bytes which I can access but can't combine to form the
> original float.
>
> Well, I sort of can. If I do it directly from the file and use
>
> fread (&Xvalue,4,1,fp);
>...
> However I want
> to unload the file to a buffer and then start parsing. I've done the reading
> into the buffer, but now I don't know how to reform my float from the data, I
> tried  taking the 4 values and &-ing them together, but that doesn't work as
> the final value I'm after is a float and I get an operand error.

You could try using a union of float and int, say,

union both
{int i, float f} u;

You could then OR (that would be better than AND, I think) and shift your
bytes together to construct u.i. Then, miraculously, u.f should hold the
float value you want.

I think this will work, but it will take a bit of fiddling about to make it work
correctly I expect.

Hope this helps,
Ian
--
Ian Miller, Dorset, UK



- Raw text -


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