Page 1 of 1

PB 4 ValH() ;Hex string to quad

Posted: Sun Feb 05, 2006 8:11 am
by Rescator
Code updated For 5.20+

This implementation uses a Prototype to ensure the routine gets a ascii hex string.
And should therefore produce same result wether Unicode is enabled or not in your program.

(If anyone knows a better way, please reply...)

Code: Select all

Prototype.q _ValH(*hex.p-ascii,len.l)
Procedure.q _ValH(hex,len)
  Protected result.l,a.l,b.l
  If len>0 And len<16
    b=0
    While len
      len-1
      a=PeekB(hex+len)
      If a<58
        a-48
      Else
        a-55
      EndIf
      result=(a<<b)+result
      b+4
    Wend
  EndIf
  ProcedureReturn result
EndProcedure
ValH._ValH=@_ValH()

test$="567587"

time=ElapsedMilliseconds()
loop=9999 ;9999=10000 loops
For x=0 To loop
  num=ValH(@test$,Len(test$))
  Delay(0) ;to avoid hogging the cpu (OS multitasking friendly)
Next
time=ElapsedMilliseconds()-time
MessageRequester("Elapsed time: x"+Str(loop+1),Str(time)+"ms",#MB_ICONINFORMATION)

;Same as above, but hogs the cpu
time=ElapsedMilliseconds()
For x=0 To loop
  num=ValH(@test$,Len(test$))
Next
time=ElapsedMilliseconds()-time
MessageRequester("Elapsed time: x"+Str(loop+1),Str(time)+"ms",#MB_ICONINFORMATION)

Posted: Sun Feb 05, 2006 12:24 pm
by Psychophanta
Hello!
Can you explain what means *hex.p-ascii
What is that for?

And i don't understand the Delay(0) meaning...

Thanks

Posted: Sun Feb 05, 2006 12:30 pm
by blueznl
http://www.xs4all.nl/~bluez/datatalk/pu ... i_and_dlls

scroll down to prototypes and pseudotypes

Posted: Sun Feb 05, 2006 12:33 pm
by Psychophanta
Thank you Blue. Your web is great, but i don't understand it. May be your examples and explanation ways are too larges... :?

Posted: Sun Feb 05, 2006 12:45 pm
by Psychophanta
Thanks! i saw it!
Seems crazy. At least for now.

Posted: Sun Feb 05, 2006 1:07 pm
by SFSxOI
Do you still have to use pointers for prototypes and pseudotypes?

Posted: Sun Feb 05, 2006 3:24 pm
by blueznl
no, if i understood the question right (have you tried it?)

Posted: Sun Feb 05, 2006 3:54 pm
by jack
that's great site blueznl, would you be willing to make an off-line version?

Posted: Sun Feb 05, 2006 5:50 pm
by blueznl

Posted: Sun Feb 05, 2006 6:06 pm
by jack
thanks blueznl :D