Code archives/Algorithms/Hex$ to Decimal

This code has been declared by its author to be Public Domain code.

Download source code

Hex$ to Decimal by Cybersed2005
This little Function will convert a string containing an hex value and return a decimal integer. Hex values do not need to be specially formatted. Ex: FF, fF, 0xff are OK. Non hex characters are simply ignored.
Function Dec(h$)

	t2$=Upper$(Trim$(h$))
	d=0
	For z=1 To Len(t2$)
		i=Instr("0123456789ABCDEF",Mid$(t2$,z,1))
		If i>0 Then d=d*16+i-1
	Next

	Return d

End Function

Comments

BlitzProg2007
You saved me an hour of work with something very effective, thanks you a lot


kronholm2009
For BlitzMax, strict:

Function Dec(h$)
	Local t2$=Upper$(Trim$(h$))
	Local d=0
	For Local z=1 To Len(t2$)
		Local i=Instr("0123456789ABCDEF",Mid$(t2$,z,1))
		If i>0 Then d=d*16+i-1
	Next
	Return d
End Function



grable2009
For blitzmax you could just as well do
Print "$FF".ToInt()

;)


Kryzon2016
Or this, if it's in a variable:
Print ( "$" + myString ).ToInt()



Code Archives Forum