Home AMX User Forum AMX General Discussion

Parsing Hex Strings

What is a recommended way to parse hex from a hex string? I am trying to do a remove_string where I remove the first byte of a 4 byte string, where it looks like this: ,$00,$04,$23,$D0. I want to remove each section and place it into its own array dimension, like so:
array[1] = ,$00
array[2] = ,$04
array[3] = ,$23
array[4] = ,$D0
My goal here is to be able to ultimately calculate these hex values into decimal, and add them all together. However, the problem I am currently having is that when I try to do this:
local_var char sTesting[5]
sTesting = (REMOVE_STRING(sProjInput,',',1))
I get no response when I debug. I tried using the double quotes, which is what I am using to strip the first portion of the command response:
REMOVE_STRING(sProjInput,"$40,$03,$00,$0D",1)
but I get a compiler error.

Comments

  • PhreaKPhreaK Posts: 966
    The commas that you use when you form a string from hex values aren't actually part of the string, they are simply part of NetLinx's code syntax. To help explain this you have to remember that a string is an array of variables of type character. When you have something like
    temp = "$48,$65,$6c,$6c,$6f,$20,$57,$6f,$72,$6c,$64,$21"
    
    this is exactly the same as
    temp = "'Hello World!'"
    
    which is also the same as
    temp[1] = $48
    temp[2] = $65
    temp[3] = $6c
    temp[4] = $6c
    temp[5] = $6f
    temp[6] = $20
    temp[7] = $57
    temp[8] = $6f
    temp[9] = $72
    temp[10] = $6c
    temp[11] = $64
    temp[12] = $21
    set_length_string(temp, 12)
    

    So in your situation, if you are simply wanting a string exploded into an array of individual character you don't actually have to do anything, you can simply use:
    sTesting[characterNum]
    

    Hope this helps.
  • viningvining Posts: 4,368
    Here's some other examples that may help:
    //        1   2   3   4   5   6   7   8   9  10  11  12
    TEMP = "$48,$65,$6c,$6c,$6f,$20,$57,$6f,$72,$6c,$64,$21"
    fnDoHandleTemp(TEMP) ;
      
    DEFINE_FUNCTION fnDoHandleTemp(CHAR iTEMP[])
    
         {
         STACK_VAR CHAR RX_DATA_COPY[24] ;
         STACK_VAR CHAR RX_DATA_REMOVE[24] ;
         STACK_VAR CHAR RX_DATA_4BYTES[24] ;
         STACK_VAR INTEGER nLen ;
         STACK_VAR INTEGER i ;
    
         nLen = LENGTH_STRING(iTEMP) ;
         
         //THIS
         RX_DATA_4BYTES = GET_BUFFER_STRING(iTEMP,4) ;
         
         //OR
         RX_DATA_4BYTES = LEFT_STRING(iTEMP,4) ;
         
         //OR
         for(i = 1 ; i<= nLen ; i++)//COPY
    	  {
    	  RX_DATA_COPY[i] = iTEMP[i] ;//COPIES THE STRING
    	  }
    	  
         //OR
         for(i = 1 ; i<= nLen ; i++)//PAC MAN
    	  {
    	  RX_DATA_REMOVE[i] = GET_BUFFER_CHAR(iTEMP) ;//EATS THE STRING
    	  }
        
         //OR 
         SELECT
    	  {
    	  ACTIVE(iTEMP[1] == $48):
    	       {
    	       //do something
    	       }
    	  ACTIVE(iTEMP[8] == $6f):
    	       {
    	       //do something
    	       }
    	  ACTIVE(iTEMP[2] == $65 && iTEMP[12] == $21):
    	       {
    	       //do something
    	       }
    	  }
         }
    
  • vegastechvegastech Posts: 369
    Thanks. The posts are helping. :) I guess what's really getting me is that I need to take bytes 5,6,7,and 8 out of a total of 9 bytes, and reconfigure them so that byte 5 becomes byte 8, 6 becomes 7, 7 becomes 6, and 8 becomes 5, kind of like this:
    local_var char sDump[10]  //trash to remove 1st portion of reply
    local_var char sTime[4][2]  //container to store each hex value
    local_var integer nTime  //container to place all values together after converted from hex, using hextoi
    sDump = REMOVE_STRING(sProjInput,"$32,$2A,$0C,$00",1)
    sTime[1] = sProjInput[4]
    sTime[2] = sProjInput[3]
    sTime[3] = sProjInput[2]
    sTime[4] = sProjInput[1]
    nTime = hextoi(sTime)
    
    The array items in sTime never get populated with the method I tried above, when I go into debug mode. That was going to be my starting point in rearranging the array items (and then later use a for loop to shrink the code)
  • ProdigalProdigal Posts: 90
    Also keep in mind that each Hex value is 8 bits

    Much like a char. (char = 8bit, integer = 16bit ...)



    So, let's say you have the following Hex String:
    string: "$4E, $32, $8A, $0D"

    You don't need a multidimensional array to store these values.

    char copy_string[4]

    copy_string[1] = string[1]
    copy_string[2] = string[2]
    copy_string[3] = string[3]
    copy_string[4] = string[4]


    You don't need to make a
    char copy_string[4][2]

    string to acommodate those Hex values
Sign In or Register to comment.