Go Down

Topic: Convert html form textarea to normal text (Read 1 time) previous topic - next topic

Krodal

Dec 06, 2012, 02:23 pm Last Edit: Dec 09, 2012, 11:31 am by Krodal Reason: 1
If I send text from an textarea to my Arduino Webserver, I get this:
Code: [Select]
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: [Select]
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: [Select]
<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.

SurferTim

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

Krodal

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: [Select]

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';
}

SurferTim

Nice! I cheat by using sscanf.
Code: [Select]
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] = ' ';
  }
}


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy