Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Need to read/convert 32-bit IEEE floating-point values
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00921059
Message ID:
00922115
Vues:
18
To everyone who helped on this tough project I want to give a very large Thank You!!

The proejct is converting Btrive data, exported as an ASCII string for each record but with the 4 byte Intigers and Floating Point values not converted. The problem was to convert those numbers to Decimal.

Here is the final code that appears to work.

To convert 4 byte Intiger:
From Sergey Berezniker with help from George Tasker

( Make sure you use the @ in the call:
lnValue = Long2Num(@mStr)

FUNCTION Long2Num(tcLong)
* Convert Long integer into VFP numeric variable
*
* Declare was moved to the top of this prg so it
* only gets called once.
* DECLARE RtlMoveMemory IN WIN32API AS RtlCopyNum ;
* Long @Dest, String @Source, Long Length
*
LOCAL lnNum
*
lnNum = 0
= RtlCopyNum(@lnNum, @tcLong, 4)
*
RETURN lnNum




To convert 4 byte Floating Point:
Thaks to Anatoliy Mogylevets for giving me the link.

This started out as the code at:
http://fox.wikis.com/wc.dll?Wiki~VFPFloatingPointDataType

I had to make some mods to get it to work with my data.

- You need all the procedures below.

PROCEDURE Float2Int(mStr)
*
* Convert a 4 byte Floating Point value (mStr) to Decimal.
* This was done to convert Btrieve data but I believe it should
* work for any IEEE Floating Point Value.
*
local cBin,cSign,cExp,cMant,nExp,nMant,cStr,mVal,nPos
*
* 1st need to swap the byte order and convert to binary (in chr format)
cBin = AscChar2Bin(substr(mStr,4,1)) ;
+ AscChar2Bin(substr(mStr,3,1)) ;
+ AscChar2Bin(substr(mStr,2,1)) ;
+ AscChar2Bin(substr(mStr,1,1))
*
_ClipText = cBin
cSign = iif(substr(cBin,1,1) = "0",.t.,.f.)
cExp = substr(cBin,2,8)
cMant = substr(cBin,10)
cStr = ""
&& Convert the Exponent
nExp = Bin2Int(cExp)- 127
&& cMant is now 25 characters long with the decimal point at 2
cMant = "1" + substr(cMant,1,nExp) + "." + substr(cMant,nExp + 1)
nPos = at(".",cMant)
mVal = Bin2Int(substr(cMant,1,nPos - 1))
cMant = substr(cMant,nPos + 1)
nPos = rat("1",cMant)
if nPos > 0 && There is some decimal portion
if nPos < 8 && Pad to 8
nPos = 8
endif
cMant = substr(cMant,1,nPos)
mVal = mVal + Bin2Int(cMant)/2^nPos
endif
*
RETURN iif(cSign,mVal,-mVal)
*
* ----
*
PROCEDURE AscChar2Bin(xInt)
local cStr,i
cStr = ""
if vartype(xInt) = "C"
xInt = asc(xInt)
endif
if vartype(xInt) != "N" OR xInt > 255 OR xInt < 0
RETURN ""
endif
for m.i = 7 to 1 STEP -1
if int(xInt/(2 ^ m.i)) > 0
cStr = cStr + "1"
xInt = xInt - (2 ^ m.i)
else
cStr = cStr + "0"
endif
endfor
nLen = len(cStr)
RETURN iif(xInt > 0,cStr + "1",cStr + "0")
*
* ----
*
PROCEDURE Bin2Int(cBin)
local i,mNum,nLen
mNum = 0
nLen = len(cBin)
for m.i = 1 to nLen
mNum = mNum + val(substr(cBin,m.i,1)) * (2 ^ (nLen - m.i))
endfor
RETURN mNum
Beer is proof that God loves man, and wants him to be happy. - Benjamin Franklin
John J. Henn
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform