Hex Question
Blitz3D Forums/Blitz3D Beginners Area/Hex Question
I have a silly Question, maybe I am just blind ATM:
If I have a string containing a Hex Number, say a$="$2e", how could I convert it to an Integer value?
I tried b=0+(a$) but it didn't work. The Problem seems to be that Blitz stops parsing the String in the automatic Type Conversion as soon as it hits a Non-numeric Character (which ich "$" here)
Thanks.
Wow, that's a big function. Thought there must be an easier Solution, however, thanks a lot!
How long can your hex number be? If it is just a 2-digit hex number, say, as in your example, you can write your own specific routine, which can be much shorter.
Here's a shorter function for a 2-digit hex:
Print hex_int("$ff")
Repeat Until KeyHit(1)
End
Function hex_int(a$)
Local tempa$,tempint,newint=0
For i=2 To 3
tempa$=Lower(Mid$(a$,i,1))
If Asc(tempa$)>96 And Asc(tempa$)<103 Then
tempint=Asc(tempa$)-87
Else
tempint=Int(tempa$)
End If
newint=newint+(16^(3-i)*tempint)
Next
Return newint
End Function
Holy convoluted code Batman!
Print hex_int("$ff")
Print hex_int("Af")
WaitKey()
End
Function hex_int(a$)
Local h$ = "123456789ABCDEF", l = Len(a$)
a$ = Upper$(a$)
Return (Instr(h$, Mid$(a$, l - 1, 1)) Shl 4) Or (Instr(h$, Mid$(a$, l, 1)))
End FunctionYAN
Love your code, Yan!
Instr() is your friend ;o)
Nice web site BTW
YAN
Defining h$, Instr, and that Shl 4!
Here is a piece that can use hexas of up to 8 ciffers, borrowed most of it.
; ID: 104
; Author: Unknown
; Date: 2001-10-15 01:37:58
; Title: Val function
; Description: Converts a string to an integer (Decimal)
; Val Function v0.1 By The Prof - For Blitzers Everywhere!
;
; **************************************************************
;
Function Val(txt$)
; Val v0.1 By The Prof - Last Compiled 14-10-01 - 22:00
; String to Integer function (Decimal).
;
t$=Upper$(txt$):l=Len(t$):Power=1
For Pos=L To 1 Step-1
V=Asc(Mid$(t$,Pos,1))-48
If (V>-1) And (V<10) ; Ensure a Zero for letters
Value=Value+(V*Power)
Power=Power*16
End If
If (V>16) And (V<23) ; Ensure a Zero for letters
Value=Value+((V-7)*Power)
Power=Power*16
End If
Next
Return Value
End Function
;
; **************************************************************