unHex a number

Blitz3D Forums/Blitz3D Programming/unHex a number

Lets say I use
number$ = Hex$(199999)
to get a hex value for 199999... How do I unhex that number later if I need to? I have looked in the Command Reference and possibly I missed it.
RZ

Print hex2dec("c000")
Input
End

Function hex2dec(hexin$)
	Local c, dec, hexval$ = "0123456789ABCDEF"
	For c=1 To Len(hexin$)
	dec = (dec Shl 4) Or (Instr(hexval$, Upper$(Mid$(hexin$, c, 1))) - 1)
	Next
	Return dec
End Function


Thankyew.... That is a hairy piece of math there! I appreciate it!

Now when I do this:
ascii$=Hex$(250000)
Print ascii$
Print hex2dec(ascii$)
Input
End

Function hex2dec(hexin$)
	Local c, dec, hexval$ = "0123456789ABCDEF"
	For c=1 To Len(hexin$)
	dec = (dec Shl 4) Or (Instr(hexval$, Upper$(Mid$(hexin$, c, 1))) - 1)
	Next
	Return dec
End Function
I don't see it work... Hmmm... Maybe if I renamed the $ variable to match your variable name...

OK itis 2:46AM here and I apologize... it was working GREAT but my brain sin't... manaña amigos
RZ

Function convertHexToInteger( H$ )
	Local temp = 0, i = 0, char = 0, num = 0, pow = 0
	H$ = Upper( H$ )
	For i = 0 To Len( H$ )-1
		char = Asc( Mid( H$, Len( H$ ) - i, 1 ) )
		; check to see if its A - F
		If char > 64 And char < 71 Then num = char - 55
		; otherwise check to see if its a digit
		If char > 47 And char < 58 Then num = char - 48
		pow = 16 ^ i
		temp = temp + num * pow
	Next
	Return temp
End Function


This should work for you.

works good Zawran... negligible difference in time to execute too... Thanks

No problem, glad I could help.

Function HexToInt(H$)
	integer=0
	For i=1 To Len(h$)
		hdigit=(Asc(Mid$(h$,i,1))-48) And $1f
		If hdigit>9 Then hdigit=hdigit-7
		integer=(integer Shl 4)+hdigit
	Next
	Return integer
End Function


What a wonderful function to add to my Userlib. Easy to use and very, very FAST!!

http://www.blitzbasic.com/logs/userlog.php?log=440&user=7138