Go Down

Topic: char to String to char conversion (Read 3884 times) previous topic - next topic

Nick Gammon

An & is normally & not a quote, but I guess you are just testing.

What you have looks OK, you can escape the single quote but don't need to .

It shouldn't crash, this test doesn't:

Code: [Select]
#include <Regexp.h>

void setup ()
{
  Serial.begin (115200);
  Serial.println ();

  // what we are searching (the target)
  char newsString [100] = "The quick &amp; fox jumps over &amp; lazy wolf";

  // match state object
  MatchState ms (newsString);

  ms.GlobalReplace ("&amp;", "'");

  // show results
  Serial.print ("Converted string: ");
  Serial.println (newsString);

}  // end of setup 

void loop () {}


To comment further I would have to see how you have newsString defined.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

YeahNo

It's still as I originally posted.

Code: [Select]
char newsString[85]; // make sure this is large enough for the largest string it must hold

And yes, the "&amp" should properly be "&amp;#39;"

Cheers,

Nick Gammon

Can I see the whole thing please? Mine works, yours doesn't. So it is something else.

Quote
And yes, the "&amp" should properly be "&amp;#39;"


Just "&amp;" - that is what you are searching for.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon

Oh, and how much data is in newsString? Can you display it before the conversion so we can see?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

YeahNo

newsString contains a news headline, selected at random, from an XML feed.

Code: [Select]
  if (client.connected()) {   
    strcpy_P(buffer, (char*)pgm_read_word(&(string_table[14]))); // "title data="
    finder.find(buffer);
    for (int i = 0; i < randomInt; i++)  {
      if (finder.getString(buffer,"\"", newsString, 84))  {
      }
    }



That's immediately followed by your example code.

Interestingly, I tried the approach suggested by robtillaart, prior to your regex solution:

Code: [Select]
void replace(char* source, char* from, char* to)
{
  uint8_t f = strlen(from);
  uint8_t t = strlen(to);
  char *p = source;

  if (t> f) return;
  while (*p != '\0')
  {
    if (strncmp(p, from, f) == 0)
    {
      strncpy(p, to, t);
      p += t;
      strcpy(p, p+f-t);
    }
    else p++;
  }
}


and it works like a charm, substituting at will.  I'd still like to get the regex method working as it's a lot more versatile, I suspect.

Nick Gammon

That's not exactly the whole code. I still don't see the definition for newsString. And when I asked "how much data?" I had in mind lengths, not "a news headline".

It's possible my code has a bug in it, maybe it copies one byte too far or something. But I need to reproduce it, and I can't just from snippets.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

YeahNo

Nick,

Sorry to be thick about it, but it's not clear to me what you need.  You're asking me for how newsString is defined, but I've posted that twice, which makes me think I'm not interpreting your questions correctly.

newsString is defined like this:
Code: [Select]
char newsString[85]; // make sure this is large enough for the largest string it must hold

Are you asking for the contents of newsString[]?  For an example of a replacement instance?  The data is being pulled from here:

http://www.google.com/ig/api?news

I'm randomly selecting <title data>, then parsing the headline.  So a typical string would be:

<title data="Casey Anthony&amp;amp;#39;s mother becomes emotional on stand"/>

which I then peel off everything outside the quotes, leaving the raw headline and that's what ends up in newsString[].

But as you can see, it contains the messiness of the smart quotes, hence the need to search & replace them.

Is that what you're after?

Again, apologies if I've misunderstood.

AWOL

Quote
Can I see the whole thing please? Mine works, yours doesn't.

That seems pretty clear to me.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

zoomkat

Quote
But the news item is not clean text.  It occasionally has "smart quotes" in it.  These appear in the feed as "&amp;amp;#39;" in the displayed feed. 

I want to replace the ugliness with a simple quote, '.

So, after searching high and low for a reliable replace function, I stumbled on the one right under my nose: String.replace.

However, the feed is dumped into char newsString[].


Back to the start. It has been my experience that the material downloaded from the net has far too many characters to be saved in total to a string on an arduino. Have you been able to capture all that is downloaded into a single string?
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Nick Gammon


Sorry to be thick about it, but it's not clear to me what you need.  You're asking me for how newsString is defined, but I've posted that twice, which makes me think I'm not interpreting your questions correctly.


I'm asking for the whole code, or at the very least the relevant part that does the string processing. You posted a snippet that shows pulling out 84 characters from the feed, you posted the definition of newsString and you say my code "immediately followed" your code.

Is there a problem with posting the whole thing? Surely the part that starts gathering the news, pulling out individual items, and then converting the quotes, can't be more than a couple of hundred lines?

You say it crashes, what are the symptoms? Can you say for sure that it crashes when processing the same data as the other method, or simply similar data?

Sometimes (well quite often) snippets seem to be fine, but in a larger context you say "hey, why are you doing things in that order?".

Just as a possible example, when you do this:

Code: [Select]
MatchState ms (newsString);

It calculates the length of the string by doing a strlen on it. If you then replace the newsString contents without doing this:

Code: [Select]
ms.Target (newsString);

... then it could be using an incorrect length. And incorrect lengths on strings can cause crashes.

I'm trying go help you here. When I asked to see "the whole thing" perhaps I should have said "the whole sketch" if that is where the misunderstanding arises.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

YeahNo

Quote
Back to the start. It has been my experience that the material downloaded from the net has far too many characters to be saved in total to a string on an arduino. Have you been able to capture all that is downloaded into a single string?
   


Yes. And reliably, too.

Go Up