Hex to Int

Monkey Forums/Monkey Code/Hex to Int

Steve Ancell(Posted 2013) [#1]
I couldn't find any function for converting a hex-string to an integer value, so I had a go at brewing my own.

Example of usage: myVal:Int = HexToInt("ABCD")

Function HexToInt:Int(hexIn:String)
	Local hexRefTable:String
	Local tokenValue:Int
	Local column:Int
	Local intOut:Int
	
	hexIn = hexIn.ToUpper()
	hexRefTable = "0123456789ABCDEF"
	
	For column = 0 To hexIn.Length() - 1
		tokenValue = hexRefTable.Find(hexIn[column .. column + 1])
		intOut = (intOut * 16) + tokenValue
	Next
	
	Return intOut
End



Goodlookinguy(Posted 2013) [#2]
There are some floating around. I believe I've seen at least two others aside from my own in my gigantic library which supports both little endian and big endian.

Edit (from a year later): I made the fastest conversion from hex-to-dec and dec-to-hex supporting both Big Endian and Little Endian. I tested for way too long and finally released it. It is faster than everything else available and it can be found here http://www.monkey-x.com/Community/posts.php?topic=8247#83171

I made it for extremely fast databuffer use. If you're looking for that code, I have it available here: https://bitbucket.org/Goodlookinguy/xaddon/src/a549b24e/basic/stdfuncs.monkey?at=default&fileviewer=file-view-default


Steve Ancell(Posted 2014) [#3]
Well, in addition to the ones you mention above, I guess there is now an additional one. ;-)