This is related, pulls any valid character out of a string.
This is a specialized VAL statement.
If user types garbage, this disregards it so it may be inappropriate.
Since it uses ASM, it may have to be updated in the future!
A look at some ASM.
This code is useful for placing strings into LONG's and displaying LONGS as money.
LONGS can be thought of as the number of pennies. Saves storage and is very fast.
The code is faster than VAL without truncating characters.
It does not have the features of VAL, it is limited to a small character set.
Code:
DECLARE FUNCTION String2Long (str AS STRING) AS LONG DECLARE FUNCTION String2Money(str AS STRING) AS STRING FUNCTION string2money(str AS STRING) AS STRING FUNCTION = USING$(",.##",string2long(str) *.01) END FUNCTION FUNCTION PBMAIN () AS LONG sEntry$ = "Power 1 BASIC, 2 is 3 great! 4 - it!" sEntry$ = INPUTBOX$("Enter anything","ASM String2Long",sEntry$) result& = String2Long(sEntry$) ? "String2Long: " + STR$(result&) + $CRLF + _ "USING: " + USING$(",.##",result& * .01) + $CRLF + _ "String2Money:" + String2Money(sEntry$) END FUNCTION 'use "012345679-" at any position to create long value 'does not terminate on invalid characters 'credits to: john gleason, paul dixon, mike trader, mike doty (me) 'http://www.powerbasic.com/support/pbforums/showthread.php?t=14373i] '-------------------------------------------------------------------------------------------- '3/29/08 Correction: These 2 lines were somehow missing in previous posting and the code GPF'ed FUNCTION string2long (sNum AS STRING) AS LONG !mov edi,sNum ;get the pointer to the string information (not the string) !mov edi,[edi] ;get the point to the string contents (added this) '-------------------------------------------------------------------------------------------- !xor edx,edx ;the sum =0 !xor ecx,ecx ;the neg flag =0 !movzx eax,byte ptr [edi] ;get the first character lp: !cmp eax,"0" ;is character < a "0"? !jl lessthan0 ;yes, skip to the non-digit check !cmp eax,"9" ;is it greater than a "9"? !jg nextcharacter ;yes, get next char. 'to get here it must be a digit !imul edx,10 ;sum=sum*10 .. !add edx,eax ; + digit !sub edx,48 ; - 48 !jmp nextcharacter lessthan0: REM !cmp eax,"." ;is it a "." 'rem to terminate on decimal REM !je done ;yes, then exit loop 'rem to terminate on decimal !cmp eax,"-" ;is it a "-" !jne nextcharacter ;no, get next character !mov ecx,1 ;set the neg flag nextcharacter: !inc edi ;increment the string pointer !movzx eax,byte ptr [edi] ;get next character !or eax,eax ;test if zero !jnz lp ;not zero, go back and do next character done: !cmp ecx,0 ;is neg flag set? !je skip ;no, skip next instruction !neg edx ;yes, negate answer skip: !mov function,edx ;write answer to function END FUNCTION ' [URL]http://www.powerbasic.com/support/pbforums/showthread.php?t=25163[/URL] 'string2long.bas ' 'purpose: 1) extract "0123456789-" from a string to create a long. ' 2) val terminates on invalid characters. this does not. ' 3) eliminate need for a string function to extract result. ' 4) fastest performance for this specific purpose. ' 5) suggestion for "optimized val for each data type" officially rejected by pb. 'val supports: 'scientific notation 'rounding of floating point values assigned to an integer 'radix 'unary operators 'overflow ' 'comments: 'add-on library, include file or dll containing assembler routines ' 'would make an excellent addition for ultimate performance. ' 'modified: 7/31/07 only updated the documention
Leave a comment: