Go Down

Topic: Replacing %3A, %2F & %3D with slashes, question marks, etc in passed get request (Read 67 times) previous topic - next topic


When passing along a URL as a Get variable, certain symbols get converted into a percentage sign, and a two digit hexadecimal equivalent of the corresponding ASCII symbol.

Therefore a colon turns into %3A, slashes are converted into %2F, question marks become %3F and equals becomes %3D. As a result the URL http://www.example.com/index.php?page=260&id=22 would end up looking something like this:


While a URL in this format will do well as a Get variable, it will not be much use as a url in it's current format. I need to decode some way. Method 1: put together a routine that replaces the %blah with it's decimal equivalent(messy), or Method 2: use magic code to convert %blah to decimal.

Any help would be great. Is there a hex to decimal function? I have done some looking but still grasping how to do this.

echo $decoded_url;



Nothing magical about it, you just need to write the code. Here is one I use all the time, you may need to adjust for your purposes.

Code: [Select]
uint8_t htoi(char c)
  c = toupper(c);
  if ((c >= '0') && (c <= '9')) return(c - '0');
  if ((c >= 'A') && (c <= 'F')) return(c - 'A' + 0xa);

boolean getText(char *szMesg, char *psz, uint8_t len)
  boolean isValid = false;  // text received flag
  char *pStart, *pEnd;      // pointer to start and end of text

  // get pointer to the beginning of the text
  pStart = strstr(szMesg, "/&MSG=");

  if (pStart != NULL)
    pStart += 6;  // skip to start of data
    pEnd = strstr(pStart, "/&");

    if (pEnd != NULL)
      while (pStart != pEnd)
        if ((*pStart == '%') && isdigit(*(pStart+1)))
          // replace %xx hex code with the ASCII character
          char c = 0;
          c += (htoi(*pStart++) << 4);
          c += htoi(*pStart++);
          *psz++ = c;
          *psz++ = *pStart++;

      *psz = '\0'; // terminate the string
      isValid = true;
Arduino Libraries https://github.com/MajicDesigns?tab=Repositories
Parola for Arduino https://github.com/MajicDesigns/Parola
Arduino++ blog https://arduinoplusplus.wordpress.com


Nick Gammon has a nice answer here.  You would call his state machine with each original char (from a for loop over the string), and it fills a second array with the decoded special chars.

If you're printing these out immediately, there's no reason to save the decoded string... Just send each char instead of putting it in an array.

His code is part of a bigger project, so you have to make your own magic code from his example. ;)



Magic code! Now all I have to do is figure it out! As is usual  I'll probably figure out something from it that makes a bunch of my existing code obsolete! haha


Go Up