delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/07/28/17:50:35

From: Clemens Valens <c DOT valens AT mindless DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Forming a float from 4 seperate bytes
Organization: http://www.remarq.com: The World's Usenet/Discussions Start Here
X-Originating-Host: 195.154.148.69
X-Wren-Trace: cKeAoLWn+5mx4/OFt+vm/O305/P48rbr8vno9ffq56f5+LLo+LLm5/ry++z/vvSz6L2qpLX2g4+ruumquOKgvOzkqaM=
Message-ID: <933149462.6683@www.remarq.com>
References: <7nku8s$hkl$1 AT nnrp1 DOT deja DOT com>
Lines: 37
Date: Wed, 28 Jul 1999 00:10:59 -0800
NNTP-Posting-Host: 10.0.3.195
X-Complaints-To: wrenabuse AT remarq DOT com
X-Trace: WReNphoon3 933149341 10.0.3.195 (Wed, 28 Jul 1999 01:09:01 PDT)
NNTP-Posting-Date: Wed, 28 Jul 1999 01:09:01 PDT
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

There is nothing miraculous about all this. It all has to
do with data representation and it is not complicated at
all. When a 4-byte float (or long int) is written to a file
it usually is in two possible formats:

StartOfFile byte1 byte2 byte3 byte4 EndOfFile

or

StartOfFile byte4 byte3 byte2 byte1 EndOfFile

When you read this into a float (what you do in your
example) then the compiler will take care of the conversion
between file format and memory format, because you told it
you wanted it read into a float. That is, if you use a
compiler that is compatible with the compiler that was used
for the program to write the float. Apparently, this is the
case for you. If not you have to swap the bytes before.

When you read the file into memory directly you can do the
same conversion yourself by using a typecast. Consider your
buffer as a buffer of bytes, go to the beginning of your
float and typecast the bytes to float, i.e.

my_float = *((float*)(&buffer[fp]));

(((I always use plenty "()")))

This will get you your float. This works in all those cases
where you have to change a sequence of bytes into C-types.

Clemens



* Sent from RemarQ http://www.remarq.com The Internet's Discussion Network *
The fastest and easiest way to search and participate in Usenet - Free!

- Raw text -


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