Parsing text srring from Hex to a value

Parsing text srring from Hex to a value

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
    • Related Articles

    • AN-332 Hexadecimal string to Integer Decimal number

      This document exposes a useful BASIC subroutine to parse a hexadecimal string value to a decimal integer and store it into a VR. It does something similar to the BASIC command VAL(“numerical_string”), which converts a string to a numerical value.
    • AN-389 VRSTRING 16 bits TrioBASIC Function

      VRSTRING combines the contents of an array of VR() variables so that they can be printed as a text string or used as part of a STRING variable. This document explains with an example code.
    • Moving CAM CSV/Text File to TABLE or VR's location

      The example attached is a Trio basic program that will extract the numerical values of each text file line and move the values into a TABLE or VR array. The program moves the data from the controllers on board memory but can be adjusted to move data ...
    • AN-341 Conversion from EURO205 to EURO205X

      The Euro 205X is the closest controller in the MC2 range to the performance and functionality of the Euro 205. This document gives the answers to the most asked questions about the upgrade path from Euro 205 to the new Euro 205X.
    • AN-296 Conversion MC206x to MC405

      The MC405 is the closest controller in the MC4 range to the performance and functionality of the MC206/MC206X. This document gives the answers to the most asked questions about the upgrade path from MC206/MC206X to the new MC405