HexToInt?

BlitzMax Forums/BlitzMax Programming/HexToInt?

Perturbatio(Posted 2005) [#1]
I've looked about a bit and can't see a way to convert a Hex string to an Int, I thought I'd ask if there was one before writing my own.


Floyd(Posted 2005) [#2]
Here' one I wrote long ago for Blitz Basic. You're welcome to Maxify it.
Function HexToInt( HexStr$ )
	HexStr$ = Upper$( Trim$ ( HexStr$ ) )
	For d = 1 To Len( HexStr$ )
		h$ = Mid$( HexStr$, d, 1 )
		n = 16 * n + Instr( "123456789ABCDEF", h$ )
	Next
	Return n 
End Function



Bot Builder(Posted 2005) [#3]
Function HexToInt( HexStr$ )
	Local n=0, tmp$=HexStr.Trim().ToUpper()
	For Local i=0 Until tmp$.Length
		If tmp[i]>47 And tmp[i]<58 Then n=(n Shl 4)+tmp[i]-48 ElseIf tmp[i]>64 And tmp[i]<71 Then n=(n Shl 4)+tmp[i]-55 Else Return n
	Next
	Return n
End Function

BMaxified and optimized :)


Perturbatio(Posted 2005) [#4]
thanks :)

(I went with botty's).


skidracer(Posted 2005) [#5]
and a teeny bit more optimized:
Function HexToInt( HexStr$ )
	Local n=0, tmp$=HexStr.Trim().ToUpper()
	For Local i eachin tmp$
		If i>47 And i<58 Then n=(n Shl 4)+i-48 ElseIf i>64 And i<71 Then n=(n Shl 4)+i-55 Else Return n
	Next
	Return n
End Function



Bot Builder(Posted 2005) [#6]
Skid, I tried that as well originally.

Actually, your code doesnt have an = between i and eachin. Even then, I got this error:

Compile Error:ForEach index variable must be an object.

My buggy version:
Function HexToInt( HexStr$ )
	Local n
	For Local c:Byte=EachIn HexStr.Trim().ToUpper()
		If c>47 And c<58 Then n=(n Shl 4)+c-48 ElseIf c>64 And c<71 Then n=(n Shl 4)+c-55 Else Return n
	Next
	Return n
End Function

Print HexToInt("abcd")
And no, throwing the c variable into the first local decleration doesn't work.


fredborg(Posted 2005) [#7]
You need to kill any prefix to make it bullet proof:
Print HexToInt("#FF")
Print HexToInt("0x837F")
Print HexToInt("$ABC")

Function HexToInt( HexStr:String )
	Local n
	HexStr = HexStr.Trim().Replace("#","").Replace("$","").Replace("0x","").ToUpper()
	For Local d:Byte = 0 Until HexStr.length
		If HexStr[d]>64 And HexStr[d]<77 Then n = (n Shl 4) + HexStr[d]-55 ElseIf HexStr[d]>47 And HexStr[d]<58 Then n = (n Shl 4) + HexStr[d]-48 Else Return n
	Next
	Return n 
End Function
And For bla:byte = EachIn StringHere doesn't work for me either. I get the same error as BotBuilder.


Perturbatio(Posted 2005) [#8]
I never knew you could use Until in that way.


Bot Builder(Posted 2005) [#9]
True, fredborg. I actually thought about this but left it off.

Even MORE optimised:
Strict

Print HexToInt("#FF")
Print HexToInt("0x837F")
Print HexToInt("$ABC")

Function HexToInt( HexStr:String )
	Local n
	HexStr = HexStr.Trim().Toupper()
	If HexStr[0]=35 Or HexStr[0]=36 Then HexStr=HexStr[1..] ElseIf HexStr[0]=48 And HexStr[1]=88 Then HexStr=HexStr[2..]
	For Local i = 0 Until HexStr.length
		If HexStr[i]>64 And HexStr[i]<77 Then n = (n Shl 4) + HexStr[i]-55 ElseIf HexStr[i]>47 And HexStr[i]<58 Then n = (n Shl 4) + HexStr[i]-48 Else Return n
	Next
	Return n 
End Function



skidracer(Posted 2005) [#10]
doh, my bad...


Bot Builder(Posted 2005) [#11]
Some related routines... I coded them for my interpreter:
Hex only checks for $ since this is designed for bmax compatability.


marksibly(Posted 2005) [#12]
h$="abc"
Print Int( "$"+h )


FlameDuck(Posted 2005) [#13]
Now that's clever!


Bot Builder(Posted 2005) [#14]
LOL!!!

Ah man...

Well, that simplifies my code XD

thanks mark...


Perturbatio(Posted 2005) [#15]
I think I'll go with Mark's code now :)


Loonie(Posted 2005) [#16]
i love it! thanks mark!


that's why he's making max and we're just coding with it ;)


Russell(Posted 2005) [#17]
Another one to add to the docs ;P

Only the Int data type is listed in the language reference :(, not the Int() function. And no search tool yet...

I know, complain complain complain... ;)

Russell


Perturbatio(Posted 2005) [#18]
I ended up wrapping it slightly for error checking:
Function HexToInt( HexStr:String )
	If HexStr.Find("$") <> 0 Then HexStr = "$" + HexStr$
	Return Int(HexStr)
End Function



Russell(Posted 2005) [#19]
Always a good idea :)

What if someone says i:Int = HexToInt("$JABD") ;P

Russell


Perturbatio(Posted 2005) [#20]
then they need a kicking.


Russell(Posted 2005) [#21]
Agreed ;)

(Actually, not tested, but probably what would happen is that either the function would return zero <error> or do as much of the conversion as it can). Zero would probably be safer\better since a 'partial' answer could cause problems!

Russell