0

I am trying to process instructions from a web page, but special characters are encoded.

Here is an example:

command=setkey
page=1
key=D
value=N%6eE8qiCZ\r

this is received as

"command=setkey&page=1&key=D&value=N%256eE8qiCZ%5Cr"

"%" is converted to "%25", "\" becomes "%5C".

Is there easy way to return "N%256eE8qiCZ%5Cr" to "N%6eE8qiCZ\r" ?

Eki
  • 75
  • 9

1 Answers1

0

Here is one solution I found with hint "urlEncode" (thanks lorro):

 //decode received web form string
unsigned char h2int(char c)
{
    if (c >= '0' && c <='9'){
        return((unsigned char)c - '0');
    }
    if (c >= 'a' && c <='f'){
        return((unsigned char)c - 'a' + 10);
    }
    if (c >= 'A' && c <='F'){
        return((unsigned char)c - 'A' + 10);
    }
    return(0);
}

String urldecode(String str)
{
    
    String encodedString="";
    char c;
    char code0;
    char code1;
    for (int i =0; i < str.length(); i++){
        c=str.charAt(i);
      if (c == '+'){
        encodedString+=' ';  
      }else if (c == '%') {
        i++;
        code0=str.charAt(i);
        i++;
        code1=str.charAt(i);
        c = (h2int(code0) << 4) | h2int(code1);
        encodedString+=c;
      } else{
        
        encodedString+=c;  
      }
      
      yield();
    }
    
   return encodedString;
}

Called like this:

String webresponse = "command=setkey&page=1&key=D&value=N%256eE8qiCZ%5Cr";
String result = urldecode(webresponse);
Serial.println(result);
Eki
  • 75
  • 9