Pages: 1 [2]   Go Down
Author Topic: char to String to char conversion  (Read 3136 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's still as I originally posted.

Code:
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,
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh, and how much data is in newsString? Can you display it before the conversion so we can see?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
  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:
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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25827
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Can I see the whole thing please? Mine works, yours doesn't.
That seems pretty clear to me.
Logged

"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.

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: 1 [2]   Go Up
Jump to: