Check out the new PString library.
PString is a very lightweight string class that derives from Print, so you can use it to render text to string buffers the same way you would to the Serial port or to a LiquidCrystal device. Try it out. Feedback welcome.
Mikal
Albert
15 years ago
Dear Mikal,
I have some improvements for the Flash, PString and Streaming libraries and I found some minor bugs. Look for *Albert in the files for the changes. You can find my files here: http://www.esnips.com/web/Arduino/
test_pstring.pde
1. I added a streaming example.
2. Other “quickie” examples: There is a bug, the first 2 lines don’t work.
I was looking for a method to get rid of the sizeof(buffer) in de constructor but I am afraid that this is not possible.
I send you the other files soon.
Yours sincerely,
Albert
Joachim
15 years ago
Hi Mikal,
Once again you’ve solved one of my challenges, writing all sorts of variables to string. But my string turns up empty in Serial.print. Here’s an extract of the code, it’s used to create a string to be written into a memory card to make a csv file:
char ShotString[80];//Buffer string to help write to memory card
PString mystring(ShotString, sizeof(ShotString));
mystring.begin(); //goes to the start of ShotString buffer
if (day<=9){mystring.print(“0”);}
mystring.print(day);
if (month<=9){mystring.print(“0”);}
mystring.print(month);
mystring.print(year);
mystring.print(“,”);
if (hour<=9){mystring.print(“0”);}
mystring.print(hour);
if (minute<=9){mystring.print(“0”);}
mystring.print(minute);
mystring.print(“,”);
mystring.print(ClubSelection);
mystring.print(“,”);
mystring.print(shotlength);// mystring.print(“;”);
Serial.println(ShotString);//This returns 0, however the length is 16!!
The strange thing is that it compiles fine, and the length may be ok, but the serial sends “0”.
Also, Am I right in starting the string as I have done with begin? If the new string is shorter than the last, will the termination handle that and leave the new string in its correct length?
Thanks again Mikal, you are a real Arduino hero!
Joachim
Mikal
15 years ago
Well, it’s not heroic if it doesn’t work! :|
Just to clarify, does the Serial.println line print nothing or “0”? What version of Arduino? Can you try deleting PString.o to make sure you are getting the library recompiled? I would expect that to work. Yes, you should use begin() if you intend to reuse/reset the string object. Perhaps try a simpler example like
mystring.print(“test”);
Serial.println(ShotString);
?
Mikal
Joachim
15 years ago
Hi again,
Now that worked in the example code, but not in my large code. I’m using IDE 16 and a 2009 328. I’ll have to look for what is different. Thanks again.
PS, here’s what the outcome is:
ShotString: 0
Size 16
Joachim
Mikal
15 years ago
Hmmm…
Could you try printing each character individually, something like
?
I wonder if somehow some null bytes are getting embedded into the string.
Mikal