Go Down

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

YeahNo


Quote
So, what is the correct way to create a String that is capable of holding the contents of newsString[]?


I doubt you will be able to capture the total feed to a string unless it is very small. You can capture the feed characters in a string and then send the string to the serial monitor to see just how much of the feed was captured.


I think this may be a distraction.  The existing code, prior to the .replace call works reliably and has done for a couple of weeks.  It's the .replace call that's the issue here.

As previously quoted, this bit of code:

Code: [Select]
if (finder.getString(buffer,"\"", newsString, 84))  {

uses the TextFinder.getstring call to stuff up to 84 characters of the current feed from the text contained in buffer[], up to the quote mark that's at the other end of the code.

In brief, the existing code works. 

The issue now is that I want to pass the resulting char newsString[] to String.replace, replace the string, then convert it back to a char[] and I'm clearly not calling String correctly in the process.

PaulS

Code: [Select]
  d = String(newsString).replace("&amp39;", "\'");
You could be running out of memory. String(newsString) makes a copy of the data in newsString. So, now, you have this unnamed copy, newsString, and buffer all containing the same up-to-84 characters, while the finder object also contains a copy of the same data.

With the Client object, the Ethernet object, etc. also using memory, you may be using more than there is. Since you can't/won't post all of you code, search for "FreeMemory" on the forum. There is a function you can add to your sketch that will tell you, at certain points in your code, how much SRAM you still have available. I'm guessing it's not much/enough.

YeahNo

Paul,

You wrote:

Quote
Quote
Code: [Select]
String d = String(85);
This creates a String object, d, equal to the String object that contains a character representation of the value 85, not one that contains room for 85 characters.


Aha.  That'll be the problem.

So, what is the correct way to create a String that is capable of holding the contents of newsString[]?


It's definitely possible that I'm running out of RAM. I'm already using techniques to move the static strings into program memory.

However, given that I'm creating d in correctly, not giving it 85 characters, but rather assigning it the value 85, isn't it more likely I'm running off the end of d?  And if that's the case, how do I create d with enough space for newsString[85]?  The String reference page shows many cases, none of which allow you create an empty string with 85 characters.  Am I really left with doing this?

Code: [Select]
String d = String("                                                                                                                                               ");

PaulS

Quote
However, given that I'm creating d in correctly, not giving it 85 characters, but rather assigning it the value 85, isn't it more likely I'm running off the end of d?  And if that's the case, how do I create d with enough space for newsString[85]?  The String reference page shows many cases, none of which allow you create an empty string with 85 characters.  Am I really left with doing this?

You are not defining d incorrectly. You are simply not defining it the way that you thought you were.

You should probably spend some time looking at the String source code. If you do, you will see that the copy operator and the assignment operator are defined for the String class, so one can assign a character array to a String object, and the String object will be automatically sized to hold the character array, whatever size the character array is, provided there is memory available to hold the String object that is to be created.

YeahNo


You are not defining d incorrectly. You are simply not defining it the way that you thought you were.

You should probably spend some time looking at the String source code. If you do, you will see that the copy operator and the assignment operator are defined for the String class, so one can assign a character array to a String object, and the String object will be automatically sized to hold the character array, whatever size the character array is, provided there is memory available to hold the String object that is to be created.


Aha.  With that in mind, this revision is probably a good idea:

Code: [Select]

  //Define d to be however big it needs to be in order to hold newsString and do a find and replace at the same time.
   String d = String(newsString).replace("&amp39;", "\'");

   //Convert d back into newsString.
    d.toCharArray(newsString, 84);


If this looks right to you, and if I'm not walking off the end of the string, then that leaves your suggestion, that I'm running out of memory, as the only plausible explanation.

So, is the code itself correct?

Go Up