Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Reading a double from a file
Message
De
02/05/1998 09:10:27
Dragan Nedeljkovich (En ligne)
Now officially retired
Zrenjanin, Serbia
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Fonctions Windows API
Divers
Thread ID:
00096119
Message ID:
00096495
Vues:
16
>Hello!
> I'm using low level functions to read (import) a binary file (I think it was created with C/C++). Of course, every thing I read becomes a character. Is there an easy way (I mean a function or something like that) to "cast" a 4-byte character to a VFP Flotant point variable, and a 8-byte to Double?
> I can do the "translation" step by step (I mean bit by bit) in VFP, but it takes too long, considering that I have to do 60,000 or so "interpretation" of this every time I read the file.

I did it in VFP. It really took two days to write and test - without any proper docs, relying only on representations of known numbers and the knowledge of how did sir Clive Sinclair's Spectrum Basic store its floats. IEEE doubles are practically the same, except for having the mantissa three bytes longer. Here's the code

****************************************
Function BtoN
*+ douBle string --> numeric
* takes a 8-byte string containing a double float
* returns a VFP numeric
*-
Lpara _s
dime bajt(8)
for j=1 to 8
bajt[j]=asc(subs(_s,j))
endf
znak=iif(bittest(bajt[8], 7), -1, 1)
eksp=bitand(bajt[8],127)*16+bitrshift(bajt[7], 4)
eksp=eksp-1023
mant=1+bitand(bajt[7], 15)/16
dv=4096
for j=6 to 1 step -1
mant=mant +bajt(j)/dv
dv=dv*256
endf
rez=mant*2**eksp*znak
retu rez

I didn't take the time to translate the var names into any convention or language - they work as they are, anyway. Didn't test its speed, though - I wrote this for the sake of my table rebuilder, which is intended to run on emergency, so the success was the issue, not the speed.

back to same old

the first online autobiography, unfinished by design
What, me reckless? I'm full of recks!
Balkans, eh? Count them.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform