Home AMX User Forum AMX General Discussion

URL Encode?

Does anyone have a function they would like to share that will encode a URL string so that it is valid? (Or a link to a topic that already discussed this).

Thanks,
Jeff

Comments

  • Here's my URLEncode function.
    DEFINE_FUNCTION CHAR[500] URLEncode(CHAR Input[500])
    {
        STACK_VAR INTEGER i
        STACK_VAR CHAR TEMP[500]
        STACK_VAR CHAR URLChars[24]
    
        URLChars = '$&+,/:;=?@ "<>#&#37;{}|\^~[]`'
        
        TEMP = ''
        
        i = 1
        FOR (i = 1; i <= LENGTH_STRING(Input); i++)
        {
    	IF (FIND_STRING(URLChars,MID_STRING(Input,i,1),1))
    	{
    	    TEMP = "TEMP,'%',FORMAT('%02X',Input[i])"
    	}
    	ELSE
    	{
    	    TEMP = "TEMP,Input[i]"
    	}
        }
        RETURN TEMP
    }
    
  • viningvining Posts: 4,368
    Here's what I used in my Axis Camera module:
    DEFINE_FUNCTION CHAR[AXIS_PRESET_NAME_LEN * 3] fnURL_Encode(CHAR iStr[]) 
    
         {
         STACK_VAR INTEGER i ;
         STACK_VAR INTEGER nEncodeByte;
         STACK_VAR INTEGER nLenURLStr ;
         STACK_VAR CHAR cURL_EncodeStr[AXIS_PRESET_NAME_LEN * 3] ;//must be bigger. you might be swapping 1 char for 3!
         
         fnAxis_DeBug("'URL Encode In: ',iSTR,'.>-line-<',ITOA(__LINE__),'>'") ;
         nLenURLStr = length_string(iStr) ;
         for(i = 1 ; i <= nLenURLStr ; i ++)
    	  {
    	  if(iStr[i] > 31 && iStr[i] < 48 || iStr[i] > 57 && iStr[i] < 65 || 
    		    iStr[i] > 90 && iStr[i] < 97 || iStr[i] > 122 && iStr[i] < 127)
    	       {
    	       nEncodeByte = iStr[i] ;
    	       cURL_EncodeStr = "cURL_EncodeStr,GET_BUFFER_STRING(iStr,i-1),'%',ITOHEX(nEncodeByte)" ;
    	       GET_BUFFER_CHAR(iStr) ;
    	       nLenURLStr = length_string(iStr) ;
    	       i = 0 ;
    	       }
    	  }
         fnAxis_DeBug("'URL Encode Out: ',cURL_EncodeStr,iStr,'.>-line-<',ITOA(__LINE__),'>'") ;
         RETURN "cURL_EncodeStr,iStr" ;
         }
    
Sign In or Register to comment.