PB 4 ValH() ;Hex string to quad

Share your advanced PureBasic knowledge/code with the community.
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

PB 4 ValH() ;Hex string to quad

Post 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)
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

http://www.xs4all.nl/~bluez/datatalk/pu ... i_and_dlls

scroll down to prototypes and pseudotypes
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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... :?
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Thanks! i saw it!
Seems crazy. At least for now.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

Do you still have to use pointers for prototypes and pseudotypes?
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

no, if i understood the question right (have you tried it?)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Post by jack »

that's great site blueznl, would you be willing to make an off-line version?
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Post by jack »

thanks blueznl :D
Post Reply