Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Getfreediskspaceex
Message
From
15/07/2020 14:29:27
 
 
To
15/07/2020 08:56:30
General information
Forum:
Visual FoxPro
Category:
Windows API functions
Miscellaneous
Thread ID:
00640820
Message ID:
01675194
Views:
60
>>FUNCTION Str2num
>>LPARAMETERS tcStr,tnLen
>>local lnRetVal
>>lnRetVal = 0
>>FOR ix=1 TO tnLen
>> lnRetVal = lnRetVal + ASC(SUBSTR(tcStr,ix))*(256^(ix-1))
>>endfor
>>RETURN TRANSFORM(lnRetVal,'999,999,999,999,999')Cetin
>
>This function can also be re-written as:
>
>
FUNCTION Str2num
>LPARAMETERS tcStr, tnLen
>LOCAL lnI, lnReturn
>    lnReturn = 0
>    FOR lnI = tnLen TO 1 STEP -1
>        lnReturn = (lnReturn * 256) + ASC(SUBSTR(tcStr, lnI))
>    NEXT
>    RETURN TRANSFORM(lnReturn, '999,999,999,999,999')
>
>
>It's a little less expensive (3.31 seconds compared to 4.13 seconds on 1M iterations), though it hardly matters.

Rick (and others),

Just a reminder that VFP has the CTOBIN() and BINTOC() functions to assist in getting or setting binary representation of numbers (including floating pointer numbers).

Their use would improve more significantly on solutions based in a byte-by-byte calculation, as in
FUNCTION Str2num_3
LPARAMETERS tcStr, tnLen
   RETURN TRANSFORM(CTOBIN(RIGHT(PADR(m.tcStr, 8, CHR(0)), 4), "4RS") * 4294967296 + CTOBIN(LEFT(PADR(m.tcStr, 8, CHR(0)), 4), "4RS"), '999,999,999,999,999')
(this typically halves the execution time of byte-by-byte calculations, and even more if TRANSFORM() is not considered).
----------------------------------
António Tavares Lopes
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform