Hex Question

Blitz3D Forums/Blitz3D Beginners Area/Hex Question

jfk EO-11110(Posted 2003) [#1]
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.


GfK(Posted 2003) [#2]
http://www.blitzbasic.com/codearcs/codearcs.php?code=219


jfk EO-11110(Posted 2003) [#3]
Wow, that's a big function. Thought there must be an easier Solution, however, thanks a lot!


robleong(Posted 2003) [#4]
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.


robleong(Posted 2003) [#5]
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


Yan(Posted 2003) [#6]
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 Function
YAN


robleong(Posted 2003) [#7]
Love your code, Yan!


Yan(Posted 2003) [#8]
Instr() is your friend ;o)

Nice web site BTW


YAN


robleong(Posted 2003) [#9]
Defining h$, Instr, and that Shl 4!


eBusiness(Posted 2003) [#10]
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
;
; **************************************************************