3

This has got to be the easiest thing to do in C++.

..and I know it's been asked many many times before, however please keep in mind that this is part of an Arduino project and memory saving is a major issue as I've only got 32256 byte maximum to play with.

I need to convert an integer to a string.

int GSM_BAUD_RATE;
GSM_BAUD_RATE = 4800;

Serial.println("GSM Shield running at " + GSM_BAUD_RATE + " baud rate.");

Obviously the last line is going to give me an error.

Thanks in advance.

4
  • 8
    You must be from a java background. Commented Nov 24, 2011 at 23:21
  • 1
    Added tags for Arduino - it makes a big difference here. Commented Nov 24, 2011 at 23:33
  • 2
    @Fulvio: The fact you're working on an Arduino was an extremely important piece of information you should've added to the question in the first place. For future reference when asking on Stack Overflow, you need to specify which platform you're working on. C++ is used on many different platforms; we will default to the standard C++ facilities if you don't say otherwise. Commented Nov 24, 2011 at 23:55
  • 1
    @Insilico Point taken for future reference. Commented Nov 24, 2011 at 23:56

6 Answers 6

6

If, as it seems, you are working on an Arduino project, you should simply let the Serial object deal with it:

int GSM_BAUD_RATE;
GSM_BAUD_RATE = 4800;

Serial.print("GSM Shield running at ");
Serial.print(GSM_BAUD_RATE);
Serial.println(" baud rate.");

since the print and println methods have overloads to handle several different types.

The other methods can be useful on "normal" machines, but stuff like string and ostringstream require heap allocation, which, on an Arduino board, should be avoided if possible due to the strict memory constraints.

Sign up to request clarification or add additional context in comments.

1 Comment

You win Matteo, thanks for noticing that it's for an Arduino project.
3

UPDATE: this answers the original question, before it was updated to mention Arduino. I'm leaving it, as it is the correct answer for non-embedded systems.

You can create a formatted string using a stringstream, and extract a string from that.

#include <sstream>

std::ostringstream s;
s << "GSM Shield running at " << GSM_BAUD_RATE << " baud rate.";

Serial.println(s.str().c_str()); // assuming `println(char const *);`

3 Comments

If my guess is correct, that code is for an Arduino project - and <sstream> is not available there (or, even if it is, it requires heap allocation, and with 2KB of total RAM it's not a good idea).
In that case you'll want one of the other answers. This is a safer option if you've got a full standard library and can afford a bit of memory allocoation.
@MikeSeymour Thanks for the answer though, I should've mentioned it was for an Arduino project originally.
1
 int i = 42;
 char buf[30];
 memset (buf, 0, sizeof(buf));
 snprintf(buf, sizeof(buf)-1, "%d", i);
 // now buf contains the "42" string.

Comments

1

You could use a stringstream:

int main()  
{
    int myInt = 12345;
    std::ostringstream ostr;
    ostr << myInt;
    std::string myStr = "The int was: " + ostr.str();
    std::cout << myStr << std::endl;
}

Comments

0

Try this one:

#include <iostream>

int GSM_BAUD_RATE; 
GSM_BAUD_RATE = 4800; 
char text[256];

sprintf(text, "GSM Shield running at %d baud rate.", GSM_BAUD_RATE);

Serial.println(text);

Comments

0

The C++ method of doing this is boost::format

std::string str = "GSM blah blah ";
str+= boost::str(boost::format("%d") % 4800);
str+= "blah blah";

4 Comments

I'm quite sure he doesn't want to use boost on an Arduino board.
It requires a working standard library, which, IIRC, is not completely available in Arduino projects. Also, std::string requires heap allocation, which isn't a good idea there.
(by the way, since when "header only" means "little runtime cost"?)
Oops, comment should be "It is header only, so there is no need to link to an external library".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.