
FB II Compiler
PG PRO
Debugging
Memory
System
Mathematics
Resources
Disk I/O
Windows
Controls
Menus
Mouse
Keyboard
Text
Fonts
Drawing
Sound
Clipboard
Printing
Communication
ASM
|
TEXT
Convert a string to handle and back
try this:
CLEAR LOCAL
LOCAL FN testStringToHandleToString
DIM myString$, myOtherString$
DIM myStrSize%, myStrSize&
DIM myStrHandle&, err%
myString$ = "This be a test!"
myStrSize% = LEN(myString$)
myStrSize& = myStrSize% 'BLOCKMOVE and FN NEWHANDLE require a long&
myStrHandle& = FN NEWHANDLE(myStrSize&)
LONG IF myStrHandle&
err% = FN HLOCK(myStrHandle&)
BLOCKMOVE (@myString$ + 1), [myStrHandle&], myStrSize&
myStrSize& = FN GETHANDLESIZE(myStrHandle&)
myStrSize% = myStrSize&
POKE @myOtherString$, myStrSize%
BLOCKMOVE [myStrHandle&], (@myOtherString$ + 1), myStrSize&
err% = FN HUNLOCK(myStrHandle&)
DEF DISPOSEH(myStrHandle&)
END IF
PRINT myString$
PRINT myOtherString$
END FN
Michael Evans
'Will run as is
LOCAL FN str2Hndl(@theStr&,theHndl&)
LONG IF theHndl&
size = PEEK(theStr&)
err = FN SETHANDLESIZE(theHndl&,size)
BLOCKMOVE theStr&+1,[theHndl&],size
END IF
END FN
LOCAL FN hndl2Str(theHndl&,@theStr&)
LONG IF theHndl&
size = FN GETHANDLESIZE(theHndl&)
IF size > 255 THEN size = 255
POKE theStr&, size
BLOCKMOVE [theHndl&],theStr&+1,size
END IF
END FN
DIM myStr$,myNewStr$
myStr$ = " It works. Click to end."
myHndl& = FN NEWHANDLE(0)
LONG IF myHndl&
FN str2Hndl(myStr$,myHndl&)
FN hndl2Str(myHndl&,myNewStr$)
PRINT myNewStr$
END IF
DO
UNTIL FN BUTTON
DEF DISPOSEH(myHndl&)
Jay
I modified your code a little to do more specifically what I wanted -- which was to create two functions that: 1) If given a string, would return a handle; 2) if given a handle, would return a string.
You might want to review the following code to see if I blundered.
Thanks again for the primer.
'--- will run as is --
COMPILE 0,_dimmedVarsOnly
END GLOBALS
'--------------------------
LOCAL FN buildWind
WINDOW 1,"String <=> Handle",(0,0)-(300,100),_docNoGrow
END FN
'--------------------------
LOCAL FN str2Hndl(@theStr&)
DIM size
DIM err
DIM theHndl&
theHndl& = FN NEWHANDLE(0)
LONG IF theHndl&
size = PEEK(theStr&)
err = FN SETHANDLESIZE(theHndl&,size)
BLOCKMOVE theStr&+1,[theHndl&],size
XELSE
theHndl& = _nil
END IF
END FN = theHndl&
'--------------------------
LOCAL FN hndl2Str$(theHndl&)
DIM size
DIM theStr$
DIM theStr&
LONG IF theHndl&
theStr& = @theStr$
size = FN GETHANDLESIZE(theHndl&)
IF size > 255 THEN size = 255
POKE theStr&, size
BLOCKMOVE [theHndl&],theStr&+1,size
XELSE
theStr$ = ""
END IF
END FN = theStr$
'--------------------------
"MAIN"
FN buildWind
DIM myStr$
DIM myNewStr$
DIM myHndl&
myStr$ = " It works. Click to end."
myHndl& = FN str2Hndl(myStr$)
myNewStr$ = FN hndl2Str$(myHndl&)
PRINT myNewStr$
DO
UNTIL FN BUTTON
DEF DISPOSEH(myHndl&)
Tedd
Looks good to me. There are a couple of insignificant changes I would
probably make, just because I'm passionate about tight code. (If you
don't make the changes, I guarantee you'll never know the difference.)
1. If FN NEWHANDLE(0) fails (hard to imagine), theHndl& will already be
_nil, so you don't really need
XELSE
theHndl& = _nil
2. As a matter of course I prefer to use
POKE @theStr$,0
instead of
theStr$ = ""
because it is faster, even though that often doesn't matter.
3. I would avoid a couple of lines and duplicate variable names by eliminating
theStr& = @theStr$
Just use @theStr$ where you have theStr&.
Here are the FNs with my edits. I've not tested them.
'--------------------------
LOCAL FN str2Hndl(@theStr&)
DIM size
DIM err
DIM theHndl&
theHndl& = FN NEWHANDLE(0)
LONG IF theHndl&
size = PEEK(theStr&)
err = FN SETHANDLESIZE(theHndl&,size)
BLOCKMOVE theStr&+1,[theHndl&],size
END IF
END FN = theHndl&
'--------------------------
LOCAL FN hndl2Str$(theHndl&)
DIM size
DIM theStr$
LONG IF theHndl&
size = FN GETHANDLESIZE(theHndl&)
IF size > 255 THEN size = 255
POKE @theStr$, size
BLOCKMOVE [theHndl&],@theStr$+1,size
XELSE
POKE @theStr$, 0
END IF
END FN = theStr$
'--------------------------
Jay
|