String concatenation of different variable types

Hi all!

I'm trying to concatenate some strings, doubles, and ints into one big string. First, I tried to convert the doubles and ints into char arrays, and then add those to strings:

  dtostre(avgTemp, 5, 2, tempString);
  int daysLeft = 21 - round(daysGoneBy);
  itoa(daysLeft, dayString, 10);
  dtostre(h, 5, 2, humidString); 
  int lastTurn = round((timeNow-timeThen)/3600000);
  itoa(lastTurn, eggTurnString, 10); */
  
  String string1="Currently, the temperature is";
  String string2="*C and the humidity is";
  String string3="%. The eggs were last turned ";
  String string4=" hours ago, and there are ";
  String string5=" days until hatch. Cheep cheep!";
  
  String FinalTweet = string1 + tempString + string2 + humidString + string3 + eggTurnString + string4 + dayString + string5;

Then, after reading this (http://arduino.cc/en/Tutorial/StringAdditionOperator), I decided to try and add them without converting them first:

  int daysLeft = 21 - round(daysGoneBy);
  int lastTurn = round((timeNow-timeThen)/3600000);
  
  String string1="Currently, the temperature is";
  String string2="*C and the humidity is";
  String string3="%. The eggs were last turned ";
  String string4=" hours ago, and there are ";
  String string5=" days until hatch. Cheep cheep!";
  
  String FinalTweet = string1 + avgTemp + string2 + h + string3 + lastTurn + string4 + daysLeft + string5;

Neither compiled. The error for the first was:

invalid conversion from int to char*
MainCode:209: error: invalid conversion from 'char*' to 'unsigned char'
MainCode:209: error: initializing argument 4 of 'char* dtostre(double, char*, unsigned char, unsigned char)'

Error for the second was:

MainCode:220: error: ambiguous overload for 'operator+' in 'string1 + avgTemp'
StringSumHelper& operator+(const StringSumHelper&, int)
StringSumHelper& operator+(const StringSumHelper&, unsigned char)
StringSumHelper& operator+(const StringSumHelper&, char)

timeNow, timeThen, daysGoneBy, avgTemp, and h are doubles
dayString, humidString, eggTurnString, and tempString are char arrays of different sizes
"MainCode" is the name of my file
Thank you!!! :slight_smile:

A lot of folks on the forum have said that the string library is really buggy and should be avoided. Here's what I do when creating some text to send out over twitter

int var1 = 100;
unsigned long var2 = 200;
char tweetMsg[100]; 
sprintf(tweetMsg, "first variable = %d, 2nd variable = %ul", var1, var2);
SendTweet(tweetMsg);

int SendTweet(char msgTweet[])
{
// code to sent out over twitter
}

After your post, I've been looking around a lot but I can't seem to find a comprehensive explanation of the sprintf function. What are these input arguments you're giving it?

sprintf(tweetMsg, "first variable = %d, 2nd variable = %ul", var1, var2)
The first is the char array you're filling, and the last two are your two variables (int and unsigned long), but what are the two middle variables (or is that just one variable since it's in the same quotes?) In other words, what is "first variable = %d, 2nd variable = %ul", and also what do %d and %ul denote?

Thanks so much! I didn't know the String library was buggy!

sprintf will substitute the value of your variable into the string. The first substitution is %d, so it takes the first number (after the string) and sticks it there. Then it sees another substitution %ul, so it takes the 2nd number it inserts that.

You use %d for integers, %ul for unsigned long integers. I think a long integer is %l.
Here's some info on the formats:
http://perldoc.perl.org/functions/sprintf.html

I don't think the Arduino supports printing floats this way.
You can also google just printf instead of sprintf. You might get more hits.

A lot of folks on the forum have said that the string library is really buggy and should be avoided.

No one has said that. The string library is very stable.

The String library, on the other hand, is a known problem.

After creating a series of strings, creating one big String seems foolish. Why not concatenate the strings into one big string?

Yes, there IS a difference between a string and a String. A monumental difference.

PaulS:
Yes, there IS a difference between a string and a String. A monumental difference.

Could you elaborate? What's the difference?

ScottG:

PaulS:
Yes, there IS a difference between a string and a String. A monumental difference.

Could you elaborate? What's the difference?

Okay, I think answered my own question. The String Class (upper case S) (see: String() - Arduino Reference) has the memory problem, or did until IDE 1.0.4. The string manipulation functions like the ones here: C Programming/String manipulation - Wikibooks, open books for an open world are okay. These are from string.h library.

Could you elaborate? What's the difference?

A string is a NULL terminated array of chars. There is a standard string library that provides functions to parse strings, to copy strings, to search strings, etc.

A String is a class that wraps a string, managing the memory needed by the array of chars. It provides methods that duplicate those in the string library.

Wow, thanks so much for the info! :slight_smile: This helps a lot.

Do I need to download the string library, string.h? I was trying to find how to get it, but I don't see it anywhere. Or is it included with the software?

Do I need to download the string library, string.h?

No.

Or is it included with the software?

It's included with the compiler that the Arduino IDE uses.