>>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