Posted: 16th Dec 2002 9:56
-Reminder made in DB not DBPRO so it may take a tweak or two to work in DBP

Hex Value Function "No strings directly used for speed"

function HexVal(hexnum$)
hexnum$=upper$(hexnum$):LENG=LEN(hexnum$):COUNT=1:V2=0
IF LENG=1 THEN GOTO SINGLE:
MORE:
NYBBLE$=mid$(hexnum$,COUNT):NYB=ASC(NYBBLE$):V=0
if val(NYBBLE$)>0 then V=Val(NYBBLE$)
if NYB>64 and NYB0 then V=Val(NYBBLE$)
if NYB>64 and NYB0 then V=Val(NYBBLE$)
if NYB>64 and NYB0 then V=Val(NYBBLE$)
if NYB>64 and NYB
Posted: 16th Dec 2002 9:59
IT DIDNT WORK LETS TRY AGIAN!

-Reminder made in DB not DBPRO so it may take a tweak or two to work in DBP

Hex Value Function "No strings directly used for speed"

+ Code Snippet
function HexVal(hexnum$)
hexnum$=upper$(hexnum$):LENG=LEN(hexnum$):COUNT=1:V2=0
IF LENG=1 THEN GOTO SINGLE:
MORE:
NYBBLE$=mid$(hexnum$,COUNT):NYB=ASC(NYBBLE$):V=0
if val(NYBBLE$)>0 then V=Val(NYBBLE$)
if NYB>64 and NYB<71 then V=(NYB-65)+10
EMR=(LENG-COUNT):V2=V2+(V*(16^EMR)):V=0
IF COUNT<LENG-1 THEN INC COUNT,1:GOTO MORE:
SINGLE:
NYBBLE$=mid$(hexnum$,LENG):NYB=ASC(NYBBLE$)
if val(NYBBLE$)>0 then V=Val(NYBBLE$)
if NYB>64 and NYB<71 then V=(NYB-65)+10
ENDFUNCTION V2+V


A 1-9 and A-Z version of HEXIDECIMAL fit larger numbers into less space "Unfortunately I cannot convert them back into the Alphanumber Value from lack of knowledge of how converting to hex works. but from to number I can
+ Code Snippet
print AlphaVal("7FZ2")
print AlphaVal("ZZ")


end
function AlphaVal(alphnum$)
alphnum$=upper$(alphnum$):LENG=LEN(alphnum$):COUNT=1:V2=0
IF LENG=1 THEN GOTO SINGLE:
MORE:
NYBBLE$=mid$(alphnum$,COUNT):NYB=ASC(NYBBLE$):V=0
if val(NYBBLE$)>0 then V=Val(NYBBLE$)
if NYB>64 and NYB<91 then V=(NYB-65)+10
EMR=(LENG-COUNT):V2=V2+(V*(36^EMR)):V=0
IF COUNT<LENG-1 THEN INC COUNT,1:GOTO MORE:
SINGLE:
NYBBLE$=mid$(alphnum$,LENG):NYB=ASC(NYBBLE$)
if val(NYBBLE$)>0 then V=Val(NYBBLE$)
if NYB>64 and NYB<91 then V=(NYB-65)+10
ENDFUNCTION V2+V 
Posted: 16th Dec 2002 15:49
Well, with such a LARGE topic name list this I was curious as to how fast this actually is.....

So I profiled the routine above against a more traditional couple of variations..



+ Code Snippet
` *=-----------------------------------------------------------------------=*
`
`  What does this do ?
`  -------------------
`
`   This is little test of Ironhoofs Hex to Dec function in the spirit of
` obtaining a speed benchmark.  So Is it really that fast ?
`
`   The test runs various loops converting a fixed 8 digit  HEX number to
` Dec. To compare against ironhoof's function I just made up a few
` varations of such a routine.  Some general purpose some more ridgid.     
`  
`   So the results? Well, it's somewhat as expected. But it's always
` good to check. While replacing existing structures with goto logic
` in other languages/situations can give a nice performance boost, it
` doesn't in this case.
`
`   The Hex_To_dec3 functions comes in a clear winner.. As expected it's
` using a lookup tables.  Hex_To_dec2 is prolly the best bet since it's
` generic and gives a pretty good speed up..  
`
`       
`
`  Cya,
`  Kevin Picone
`  www.uwdesign.50megs.com
`
` *=---------------------------------------------------------------------=*



sync on
sync rate 0


` Number of tests the trial is held over

	Convert_tests=1000


` Init the Loop up table Hex To dec 3
	gosub Init_Faster_hex_table



` The TEST LOOp
` ==============

do
	 cls 0


	hexnum$="F2F4F6F8"




	print "Testing HEX Value";HexNum$
	print "Number of tests;";Convert_tests
	print

	Print "Testing Ironhoofs Fast Hex To Dec Routine:"
	ts=timer()
		for tests=0 to Convert_tests
			r=HexVal(hexnum$)
		next tests
	Gosub Show_results



	Print "Testing Simple Hex To Dec Routine V1:"

	ts=timer()
		for tests=0 to Convert_tests
			r=Hex_String_To_Dec(hexnum$)
		next tests

	Gosub Show_results




	Print "Testing Simple Hex To Dec Routine V2: With if/then opt"
	ts=timer()
		for tests=0 to Convert_tests
			r=Hex_String_To_Dec2(hexnum$)
		next tests
	Gosub Show_results




	Print "Testing Simple Hex To Dec Routine V3: pre-calc"

	ts=timer()
		for tests=0 to Convert_tests
			r=Hex_String_To_Dec3(hexnum$)
		next tests
	Gosub Show_results





	sync


loop





Show_results:
		te=timer()
		print "Conversion Result:";r
		print "Test Time:";te-ts
		print
	return




` ------------------
` HExVal by Ironhoof
` ------------------



function HexVal(hexnum$)
	hexnum$=upper$(hexnum$):LENG=LEN(hexnum$):COUNT=1:V2=0
	IF LENG=1 THEN GOTO SINGLE:
MORE:
	NYBBLE$=mid$(hexnum$,COUNT):NYB=ASC(NYBBLE$):V=0
	if val(NYBBLE$)>0 then V=Val(NYBBLE$)
	if NYB>64 and NYB<71 then V=(NYB-65)+10
	EMR=(LENG-COUNT):V2=V2+(V*(16^EMR)):V=0
	IF COUNT<LENG-1 THEN INC COUNT,1:GOTO MORE:
SINGLE:
	NYBBLE$=mid$(hexnum$,LENG):NYB=ASC(NYBBLE$)
	if val(NYBBLE$)>0 then V=Val(NYBBLE$)
	if NYB>64 and NYB<71 then V=(NYB-65)+10
ENDFUNCTION V2+V






` *=---------------------------------------------------------------------=*
`						>> Hex To Dec Routines By Kevin Picone <<
` *=---------------------------------------------------------------------=*
`



Function Hex_String_To_Dec(hexstring$)

 Result=0
 BaseMult=1
 HexString$=lower$(hexstring$)
 If Len(hexstring$)>0
   If Mid$(hexstring$,1)="$" Then hexstring$=Right$(hexstring$,Len(hexstring$)-1)

   For lp=Len(hexstring$) To 1 Step -1
     c=Asc(Mid$(hexstring$,lp)) 
     If c>47 And c<58 Then result=result+(basemult*(c-48))
     If c>96 And c<103 Then result=result+(basemult*(c-87))
     basemult=basemult*16
   Next lp 
 EndIf

EndFunction result







Function Hex_String_To_Dec2(hexstring$)

 Result=0
 BaseMult=1
 HexString$=lower$(hexstring$)
 If Len(hexstring$)>0
   If Mid$(hexstring$,1)="$" Then hexstring$=Right$(hexstring$,Len(hexstring$)-1)

   For lp=Len(hexstring$) To 1 Step -1
     c=Asc(Mid$(hexstring$,lp)) 
	  if c<58
	     	If c>47 Then result=result+(basemult*(c-48))
	  else
     		If c>96 And c<103 Then result=result+(basemult*(c-87))
	  endif
     basemult=basemult*16
   Next lp 
 EndIf

EndFunction result


`
`
` Using a reverse lookup from the chr values avoids the inner loop calcs
`
`

Init_Faster_hex_table:

	` Assumes 32bit Max len (8 nibbles)

		MaxNibLen=8

		Dim HexConvertTable(MaxNibLen,256)

		BaseMult=1
		For Baselp=MaxNibLen to 1 step -1

			For clp=48 to 57
				HexConvertTable(Baselp,clp)=(basemult*(clp-48))
			next clp

			For clp=97 to 102
				HexConvertTable(Baselp,clp)=(basemult*(clp-87))
			next clp

		    basemult=basemult*16

		next BaseLP

	return



Function Hex_String_To_Dec3(hexstring$)

 Result=0
 BaseMult=1
 StringLen=Len(hexstring$)
 HexString$=lower$(hexstring$)

 If StringLen>1
	   If Mid$(hexstring$,1)="$" Then hexstring$=Right$(hexstring$,StringLen-1)

		BaseOffset=8-StringLen
	   For lp=StringLen To 1 Step -1
	   	result=result+HexConvertTable(BaseOffset+lp,Asc(Mid$(hexstring$,lp)))
	   Next lp 
	else
	 If StringLen=1
   	result=HexConvertTable(8,Asc(hexstring$))
	endif
 EndIf

EndFunction result

Posted: 16th Dec 2002 21:02
Ah, no need to get cocky, I mearly stated it as faster than my old one. and indeed it is. Which is all I claimed. Also be it I didn't know people already had a function to do it which is why I wasted my time to make a HEX-VAL function in the first retrospect. Therefore I wasn't comparing it to anyone elses for I didn't know anyone else even had one.
Thats that in a nutshell.