Pages: [1]   Go Down
Author Topic: Convert html form textarea to normal text  (Read 811 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Edison Member
*
Karma: 9
Posts: 1016
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If I send text from an textarea to my Arduino Webserver, I get this:
Code:
Hello%2C+this+is+a+test.%0D%0ALine+2%0D%0ALine+3
But I would like to write normal text to a file on the SD card, like this:
Code:
Hello, this is a test
Line 2
Line 3

Is there conversion code somewhere ?


I use an Arduino Mega 2560 with Ethernet Shield.
My webpages are located on the SD card.
My form textarea is:
Code:
<form method=get action=?>
<textarea name="4" rows="2" cols="40"></textarea>
<input type=submit value=submit>
</form>
The name "4" is the command.

Edit: The name "4" was a bad idea, because it is an numer. I changed it to "D". The number "4" causes a conflict when prefilling the textarea with text.
« Last Edit: December 09, 2012, 05:31:28 am by Krodal » Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 146
Posts: 6026
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It is url encoded. You must decode it. Here are the conversions:
http://www.w3schools.com/tags/ref_urlencode.asp
Logged

Offline Offline
Edison Member
*
Karma: 9
Posts: 1016
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks SurferTim,

After reading that, it was not hard.
I wrote a function to decode it. After the function I also write a "\r\n" to the file.

Code:
void URLdecode (char *pText)
{
  // The text is converted in the same string.
  // So no extra memory is needed.

  uint8_t hex;
 
  char *pRead = pText;
  char *pWrite = pText;
 
  while (*pRead != '\0')
  {
    if (*pRead == '+')
    {
      // A '+' character is a space.
      *pWrite++ = ' ';
      pRead++;
    }
    else if (*pRead == '%')
    {
      // Read two hexadecimal digits
      // Assume the hexadecimal notation is in capitals.
      // No testing is done for illegal characters.
      // If the buffer was full, the line could be truncated,
      // so testing is done for the end of line.
      pRead++;
      if (*pRead < 'A')
        hex = (*pRead - '0') * 16;
      else
        hex = (*pRead - 'A' + 10) * 16;
     
      if (*pRead != '\0')
        pRead++;
       
      if (*pRead < 'A')
        hex += (*pRead - '0');
      else
        hex += (*pRead - 'A' + 10);
     
      *pWrite++ = hex; 
     
      if (*pRead != '\0')
        pRead++;
    }
    else
    {
      *pWrite++ = *pRead++;
    }
  }
  *pWrite = '\0';
}
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 146
Posts: 6026
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Nice! I cheat by using sscanf.
Code:
void urlDecode(char* urlPtr) {
  char charBuf[3] = {0,0,0};
 
  for(int i=0;i<strlen(urlPtr);i++) {
   
    if(urlPtr[i] == '%') {
      int newLen = strlen(urlPtr);
      charBuf[0] = urlPtr[i+1];
      charBuf[1] = urlPtr[i+2];

      sscanf(charBuf,"%x",&urlPtr[i]);

      for(int x=i+1;x< (newLen-1);x++) {
        urlPtr[x] = urlPtr[x+2]; 
      }
    }
    else if(urlPtr[i] == '+') urlPtr[i] = ' ';
  }
}
Logged

Pages: [1]   Go Up
Jump to: