HexToInt?
BlitzMax Forums/BlitzMax Programming/HexToInt?
| ||
| 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. |
| ||
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 |
| ||
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 :) |
| ||
| thanks :) (I went with botty's). |
| ||
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 |
| ||
| 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. |
| ||
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 FunctionAnd For bla:byte = EachIn StringHere doesn't work for me either. I get the same error as BotBuilder. |
| ||
| I never knew you could use Until in that way. |
| ||
| 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 |
| ||
| doh, my bad... |
| ||
| Some related routines... I coded them for my interpreter: Hex only checks for $ since this is designed for bmax compatability. |
| ||
| h$="abc" Print Int( "$"+h ) |
| ||
| Now that's clever! |
| ||
| LOL!!! Ah man... Well, that simplifies my code XD thanks mark... |
| ||
| I think I'll go with Mark's code now :) |
| ||
| i love it! thanks mark! that's why he's making max and we're just coding with it ;) |
| ||
| 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 |
| ||
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
|
| ||
| Always a good idea :) What if someone says i:Int = HexToInt("$JABD") ;P Russell |
| ||
| then they need a kicking. |
| ||
| 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 |