String Class

19 Oct 2010

Is there a string class available on the Mbed?

19 Oct 2010 . Edited: 19 Oct 2010

Hi Ian

Yep, you can use the C++ string library.

Regards
Daniel

19 Oct 2010
user Daniel Peter wrote:

Hi Ian

Yep, you can use the C++ string library.

Regards
Daniel

Thanks!

 

ummm how do i include it so that I can use it?

19 Oct 2010 . Edited: 19 Oct 2010

Here's a short demo:

 

#include <string> 
using namespace std;
int main()
{
  string a("text");
  string b;
  b = a;
  b += " some more text";
  a = "new text"; 
  printf("first string: '%s', second string: '%s'\n", a.c_str(), b.c_str());
}

 

19 Oct 2010

user Igor Skochinsky wrote:
a.c_str()
Many thanks!

24 Apr 2016

How can we convert float to character array?

25 Apr 2016

Do you mean you want the float as text or as 4 bytes of binary data?

float number = 2.34;

// as binary data, data will change as number does.
char *binaryData = (char *)&number;

//as binary data, makes a copy of the current value.
char binaryData2[4];
memcpy(binaryData2, &number, 4);

// as text
char textString[16];
int length = snprintf( textString, 16, "%f", number);

snprintf will always add the null terminator to the end of the string which means that in the example above the maximum number of characters would be 15. If that isn't long enough then the output will be truncated. Length will be the length needed for the string, if the number is > 16 then you know there wasn't space for everything. All of the normal printf options can be used e.g. %.2f to round to two decimal places.