Change a Hexadecimal String to a value using the BASIC Function Library.
' Function to convert a HEX number string to a value
' Call: hex_to_val(hex_string, string_size)
' e.g. x = hex_to_val("38E38D", 6)
'
FUNCTION hex_to_val(hex_string AS STRING, str_size AS INTEGER) AS INTEGER
DIM i, ch_posn, ret_value AS INTEGER
DIM charstring AS STRING(1)
ret_value = 0: ch_pos = 0
FOR i = str_size - 1 TO 0 STEP - 1
charstring = MID(hex_string, i, 1)
ret_value = ret_value + char_to_val(charstring, ch_pos)
ch_pos = ch_pos + 1
NEXT i
RETURN ret_value
ENDFUNC
' Function to convert one character to a weighted value
' Character is sent as a single character string
' Returns an integer value that is character_value * 2 ^ character_position
' Call: char_to_val(character_string, position)
' e.g. x = char_to_val("A", 3)
'
' If the ASCII character is not 0..9 or A..F then 0 is returned
' (Upper case letter only)
'
FUNCTION char_to_val(ch AS STRING, posn AS INTEGER) AS INTEGER
DIM ch_asc, weight AS INTEGER
weight = 2 ^ (posn * 4)
ch_asc = ASC(ch)
IF ch_asc < $3a AND ch_asc > $2F THEN
RETURN(ch_asc - $30) * weight
ELSEIF ch_asc < $47 AND ch_asc > $40 THEN
RETURN(ch_asc - 55) * weight
ELSE
RETURN 0
ENDIF
ENDFUNC
====================================================================================
Here is an example program calling the function:
DIM hexstring AS STRING(10)
DIM x AS INTEGER
hexstring = "01FFFF"
x = hex_to_val(hexstring, 6)
PRINT x