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.
HexToInt?
BlitzMax Forums/BlitzMax Programming/HexToInt? thanks :)
(I went with botty's).
(I went with botty's).
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:
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:
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:
Function NumberToLong:Long( Text:String ) Text = Text.Trim() Local n:Long, sign If Text[0]=45 Then sign=-1;Text=Text[1..] Else sign=1 For Local i = 0 Until Text.length If Text[i]>47 And Text[i]<58 Then n= (n * 10) + Text[i]-48 Else Return n*sign Next Return n*sign End Function Function HexToLong:Long( HexStr:String ) Local n:Long HexStr = HexStr.Trim().Toupper() If HexStr[0]=36 Then HexStr=HexStr[1..] '$ 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 Function BinToLong:Long( BinStr:String ) Local n:Long BinStr = BinStr.Trim() If BinStr[0]=37 Then BinStr=BinStr[1..] '% For Local i = 0 Until BinStr.length If BinStr[i]>47 And BinStr[i]<50 Then n = (n Shl 1) + BinStr[i]-48 Else Return n Next Return n End Function Function DecimalToDouble:Double(Text$) Local n:Double,Dec,Sign Text = Text.Trim() If Text[0]=45 Then sign=-1;Text=Text[1..] Else sign=1 For Local i = 0 Until Text.Length If Dec<0 Then If Text[i]>47 And Text[i]<58 Then n:+(Text[i]-48)*(10.0^Dec) Else Return n*Sign Dec:-1 Else If Text[i]>47 And Text[i]<58 Then n=(n*10)+Text[i]-48 ElseIf Text[i]=46 Then Dec=-1 Else Return n*Sign EndIf Next Return n*Sign End Function Function ScientificToDouble:Double(Text$) Text = Text.Trim() Return DecimalToDouble(Text[0..Text.Find("e")])*(10^Int(NumberToLong(Text[Text.Find("e")+1..]))) End Function Print 5.2e4 Print ScientificToDouble("5.2e4") Print 2.3456 ':O Print 1+DecimalToDouble("1.3456") Print %10101011 Print BinToLong("%10101011") Print $FFA9 Print HexToLong("$FFA9") Print 534 Print 100+NumberToLong("434")Hex only checks for $ since this is designed for bmax compatability.
h$="abc"
Print Int( "$"+h )
Print Int( "$"+h )
Now that's clever!
LOL!!!
Ah man...
Well, that simplifies my code XD
thanks mark...
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 ;)
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
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
Always a good idea :)
What if someone says i:Int = HexToInt("$JABD") ;P
Russell
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
(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