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 ...
16
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 ...
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());
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));
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 ...
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 ...
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 ...
7
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.
...
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, &...
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.
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 ...
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....
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.
...
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 ...
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 ...
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"
...
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 ...
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 ...
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", "...
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, ...
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 ...
Community wiki
5
votes
Accepted
Simple URL decoding
The fact that you're using a String (urgh, shudder shudder... you should read this: http://hacking.majenko.co.uk/the-evils-of-arduino-strings) actually makes it a little harder to work with. Instead ...
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 \...
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 ...
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 = ...
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 ...
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 ...
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, ...
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, ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
string × 321arduino-uno × 67
serial × 66
c++ × 40
programming × 37
array × 32
esp8266 × 25
arduino-ide × 20
c-string × 18
c × 16
arduino-mega × 13
memory × 13
esp32 × 11
serial-data × 11
float × 10
pointer × 9
convert × 9
arduino-nano × 8
lcd × 7
data-type × 7
json × 7
softwareserial × 6
communication × 6
sd-card × 6
memory-usage × 6