Hot answers tagged

90 votes
Accepted

How do I split an incoming string?

Contrarily to other answers, I'd rather stay away from String for the following reasons: dynamic memory usage (that may quickly lead to heap fragmentation and memory exhaustion) quite slow due to ...
user avatar
  • 8,809
40 votes

How do I split an incoming string?

This function can be used to separate a string into pieces based on what the separating character is. String xval = getValue(myString, ':', 0); String yval = getValue(myString, ':', 1); Serial....
user avatar
24 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 ...
user avatar
  • 101k
14 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 ...
user avatar
  • 101k
13 votes

How do I split an incoming string?

You could do something like the following, but please take into account several things: If you use readStringUntil(), it will wait until it receives the character or timeouts. Thus, with your current ...
user avatar
  • 1,364
11 votes
Accepted

How do I send a string to master using i2c

data is a String. "test" is a char*. Wire.write() has no prototype that takes a String. Wire.write(data.c_str());
user avatar
8 votes

How do I split an incoming string?

You can use Stream.readStringUntil(terminator) passing a different terminator for each part. On each part you then call String.toInt
user avatar
8 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));
user avatar
8 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());
user avatar
  • 101k
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 ...
user avatar
  • 251
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.
user avatar
  • 101k
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 ...
user avatar
  • 101k
7 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 ...
user avatar
  • 101k
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....
user avatar
  • 35.5k
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. ...
user avatar
  • 101k
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 ...
user avatar
  • 14.4k
6 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, &...
user avatar
  • 7,811
6 votes
Accepted

Concatenation of non constant character array with a string

char clat[10+11+1] = "Latitude: "; dtostrf(gps.location.lat(), 11, 6, clat+10); Since you've named the buffer 'clat' I'm assuming it will always be used for latitude, so we've pre-initialized its ...
user avatar
  • 14.4k
6 votes
Accepted

Convert a Bitstring into an integer value

If you only need the string for printing you can store value in an integer and then use the Serial.print(number,BIN) function to format the output as a binary value. Appending integers to strings is ...
user avatar
  • 2,090
6 votes

Program got so many errors about expected ) or ; before either

Here's the first line of your code: #define dw 8; //buttons down Here's the first error line: pinMode(dw, INPUT); Let's put them together and see what we get: pinMode(8; //buttons down, INPUT); ...
user avatar
6 votes
Accepted

What is the memory expense of creating a String from a char array?

What would be the best-practice in this kind of situation? To not use String. I also want to allow the end user to use the String functions (like indexOf) to search for particular content in emails. ...
user avatar
  • 1,964
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 ...
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" ...
user avatar
6 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 ...
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 ...
user avatar
  • 1,290
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 ...
user avatar
  • 14.4k
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", "...
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, ...
user avatar
  • 16k
5 votes

Convert a Bitstring into an integer value

Check out strtoul() It should work something like this: unsigned long result = strtoul(bitstring.c_str(), NULL, 2); Now you have a long variable which can be converted into an int if needed.
user avatar
  • 201
5 votes
Accepted

float precision in arduino

The simplest way to get more decimal places to print is this: Serial.println(val,NUMBER_OF_DECMIAL_PLACES); This will print out any number of digits after the decimal place. Regular print without the ...
user avatar
  • 4,299

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