25 votes

Using String instead of C string, yet another attempt to touch a loaded issue

It's not that String itself is evil, it's more that it's very easy to abuse it and cause heap fragmentation. Used judiciously it's fine and a useful tool. Used in "bad" ways and you open the ...
Majenko's user avatar
  • 105k
17 votes
Accepted

Convert int to char[]

itoa is the best route: int num = 1234; char cstr[16]; itoa(num, cstr, 10); -> "1234" You can read more about using it here. If you want more control over the format of the number (and don't ...
Majenko's user avatar
  • 105k
11 votes
Accepted

Arduino Convert std:string to String

Don't. Instead just access the underlying C string: Serial.print(F("Name -> ")); Serial.println(founded_dev.getName().c_str());
Majenko's user avatar
  • 105k
9 votes

How do I print multiple variables in a string?

I only use this for debugging but: int a = 10; int b = 20; Serial.println("a = " + String(a) + " and b = " + String(b));
linhartr22's user avatar
8 votes
Accepted

How do string pointers work in Arduino?

String is not a simple type like an int or a char. It is a class with many member functions and, more importantly, operators. When you create the object it allocates room for that object either on ...
Majenko's user avatar
  • 105k
8 votes
Accepted

How do you convert a formatted print statement into a string variable?

To achieve what you want you probably want to use the "string format time" function strftime (docs). You would write the result in a character buffer, which you can also print directly without having ...
Maximilian Gerhardt's user avatar
8 votes

Using String instead of C string, yet another attempt to touch a loaded issue

The short answer is that fragmentation crashes are infrequent because they require very specific conditions to arise—and that's the problem. Imagine you have a crash that occurs once every few days ...
Artelius's user avatar
  • 251
7 votes

How do I split an incoming string?

Simplest solution is to use sscanf(). int id1, id2, id3; int pos1, pos2, pos3; char* buf = "1:90&2:80&3:180"; int n = sscanf(buf, "%d:%d&%d:%d&%d:%d", &id1, &pos1, &...
Mikael Patel's user avatar
  • 7,969
7 votes

Send multiple sensor values over serial

Is there any other way to send more than 100 values in one message? Yes. Don't use String. Don't use concat(). Just print each bit as a separate Serial.print() function call.
Majenko's user avatar
  • 105k
7 votes
Accepted

How to convert array of chars to array of ints?

If you have a C string containing ASCII numbers with a common delimiter (comma in this case) you can use the strtok() function to split it into individual strings. Then you can use atoi() to convert ...
Majenko's user avatar
  • 105k
7 votes
Accepted

Local char* - keeps its value

char *res = "up_cmd0_res:"; In principle, res should be a const char *. Const because it is pointing to a literal string, and you cannot (ar at least, you are not supposed to) change a literal string....
Edgar Bonet's user avatar
  • 43.1k
7 votes
Accepted

formatting strings in Arduino for output

If you need the result in a single string then your 3rd option is the preferred way. If you don't, then the first option of printing each part separately is the most efficient in terms of memory. ...
Majenko's user avatar
  • 105k
7 votes
Accepted

Why is 'n' parameter of snprintf ignored?

snprintf() will not write more than <size> (snprintf's 2d argument) characters to your buffer, but it does count (and discard the extra) characters it would have written, had there been space ...
JRobert's user avatar
  • 15.3k
6 votes
Accepted

Get strings from Serial.read()

You're adding the ASCII value of each char to your string, hence you get numbers. See the various String constructors at: https://www.arduino.cc/en/Tutorial/StringConstructors Either cast the read ...
Gee Bee's user avatar
  • 771
6 votes
Accepted

Split string into an array of string

You could try to use strtok. Code from my mind and not tested: #include <stdio.h> #include <string.h> int main(int argc, char *argv[]) { char string[] = "AT+CMGL=\"ALL\"\n" "\n" ...
salomonderossi's user avatar
6 votes
Accepted

char array comparison

PubSubClient::setServer(const char * domain, uint16_t port) just saves the pointer, it doesn't copy the string (probably because it expects a static string literal). (Source) In your case, when ip ...
tttapa's user avatar
  • 1,320
6 votes

Local char* - keeps its value

Only the pointer, *res, is a local variable. The string "up_cmd0_res:" itself is elsewhere in RAM, stored as a literal and not meant to be modified. Your strcat() call overwrites (extends) the ...
JRobert's user avatar
  • 15.3k
6 votes
Accepted

How to define and use a list/array of chars using `Serial.print`

You are almost there: Use a char* instead of char, a char can only contain one character Use " instead of ' (Thanks Juraj) Then you get: const char* months[] = {"Jan", "Feb", "Mar", "Apr", "May", "...
Michel Keijzers's user avatar
6 votes
Accepted

How to compare a string

version using String (not recommended, but it makes simpler to understand the following C-string version) #define LED 2 const char* a = "abcd"; void setup() { Serial.begin(115200); pinMode(LED, ...
Juraj's user avatar
  • 18k
6 votes

Convert String to IPAddress

The IPAddress class has a member function bool fromString(const char *address). const char *apipch = "192.168.4.1"; IPAddress apip; if (apip.fromString(apipch)) { // try to parse into the ...
5 votes
Accepted

How to append float value of into a string ?

This has been answered in the previous question but I can repeat it here: void loop() { ... float latitude = 33.546600; float longitude = 75.456912; String buf; buf += F("your location is \...
Mikael Patel's user avatar
  • 7,969
5 votes

eval() function for Arduino?

TinyExpr does what you want, and more. TinyExpr is a very small recursive descent parser and evaluation engine for math expressions. It's handy when you want to add the ability to evaluation math ...
Eric Duminil's user avatar
5 votes
Accepted

Sending string over serial to nodejs app. Why does it get chunked?

It seems I needed to add a parser to SerialPort nodejs module, in order to specify how the input should be treated. I changed my code to this: var SerialPort = require('serialport') var Readline = ...
jotadepicas's user avatar
5 votes
Accepted

Very Basic Arduino Uno Serial.readString() operation

If you set your Serial Monitor's line ending to "Both NL & CR", then this code will find the word "send". It will NOT find the word "send" in this string, "ssend" or in this string, "sendd". void ...
VE7JRO's user avatar
  • 2,565
5 votes
Accepted

Ubuntu class string has no member remove

The remove method was added to the String class in 1.0.6. Installing Arduino IDE from the Ubuntu repository gets you a really old package (1.0.5). You should download and install the newest version ...
gre_gor's user avatar
  • 1,682
5 votes
Accepted

Does the StringObject.reserve() function prevent memory fragmentation/leaks?

Reserving space will help reduce memory fragmentation, but much of what else you are doing is swamping that small improvement you have made. The biggest area of concern is your cutString function, ...
Majenko's user avatar
  • 105k
5 votes
Accepted

Using c_str() on String for IPAddress with NTPClient provides wrong values

The String object created as return from IPAddress.toString() as parameter to constructor of NtpClient is temporary. It contains the char array returned by c_str(). NTPClient doesn't copy the string, ...
Juraj's user avatar
  • 18k
5 votes

Yet another basic and silly question about C++ strings

The return value of strcmp does not have boolean semantics, as you seem to incorrectly assume. strcmp is a tri-state comparator, which returns negative, zero or positive value. For equal strings it ...
AnT stands with Russia's user avatar
5 votes
Accepted

Cannot compile F() macro with R"string"

If you put: #define DO_NOTHING(s) (s) DO_NOTHING(R"xyz(Hello World)xyz") you can see g++ choke on preprocessing the raw string literal in the macro invocation with the latest ...
timemage's user avatar
  • 5,231
5 votes

Using String instead of C string, yet another attempt to touch a loaded issue

I'd like to answer your question without discussing the more technical side. This has been admirably covered by my learned colleagues in their own answers. I am going to address the cost-benefit side ...
Nick Gammon's user avatar
  • 38.2k

Only top scored, non community-wiki answers of a minimum length are eligible