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
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 ...
Majenko's user avatar
  • 105k
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
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
6 votes
Accepted

Max length of char arrays?

The main issue with your code is: sizeof(s) That doesn't return the string length - it returns the size of the char pointer - which is two bytes (on an 8-bit system, 4 on a 32-bit system). Instead ...
Majenko's user avatar
  • 105k
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

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
  • 17.9k
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.1k
4 votes

String() vs char for simple flow control

Using Serial.readString() will make your program very slow. This is because readString() does not know when to stop reading. When the serial input buffer gets empty, it will wait just in case more ...
Edgar Bonet's user avatar
  • 42.8k
3 votes

formatting strings in Arduino for output

I agree with Majenko's answer. I created a simple CStringBuilder class to combine the first and third approach mentioned in your question. It enables to build the c-string with printf and with Print ...
Juraj's user avatar
  • 17.9k
3 votes
Accepted

Does toCharArray prevent a String being created on the heap?

The return value of getString allocates and the deletes a char array in heap for a temporary String object. For ESP8266HTTPClient and ESP32 HTTPClient library you can use writeToStream or getStream()...
Juraj's user avatar
  • 17.9k
3 votes

Problems on convert byte[] to String

If you change str += String(buffer[i]); to str += (char)buffer[i];, it will print the ascii characters to the serial monitor. The following sketch can be used to see the difference in binary compiled ...
VE7JRO's user avatar
  • 2,558
3 votes

Problems on convert byte[] to String

byte and char are the same. If you set 0 as string terminator after last character in the buffer, you get a zero terminated string. If you really must use String, you can create an instance with a ...
Juraj's user avatar
  • 17.9k
3 votes

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

I'm not an Arduino programmer, but I am a C programmer. And the problem of string types in C -- or, rather, the absence of a first-class string type in C -- is an old but important one. The ...
Steve Summit's user avatar
3 votes

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

Mega point is: don't use String (or anything else that uses the heap) inside interrupts - even with objects created outside the interrupt. the underlying memory allocation functions cannot deal with ...
Dan Gordon's user avatar
2 votes

formatting strings in Arduino for output

The wrong thig with option 3 is the buffer length, you should use snprintf instead. The other problem is that first you build the string, then you print the output, the program traverses the string ...
Jan Turoň's user avatar
2 votes

formatting strings in Arduino for output

The printf() family of functions is far preferable to me as the programmer, but in a resource-constrained environment such as the smaller AVR (and other) processors, the extra code space may not be ...
JRobert's user avatar
  • 15.2k
2 votes
Accepted

How to replace String objects with char arrays while still using string methods

You already received good answer about some general ideas for replacing String objects with C strings. Here I will try to add some more tricks more specifically targeted to your actual situation. I ...
Edgar Bonet's user avatar
  • 42.8k
2 votes

How to replace String objects with char arrays while still using string methods

So here is code to get you started: myMessageArray [256] = {'\0'}; // Define global array large enough and zero terminate the + method is replaced by strcpy (myMessageArray, "Text to Add"); // ...
Codebreaker007's user avatar
2 votes

strcmp doesn't seem to work

The easier way to simply the code is to use strcpy to create a buffer that is 1 byte longer than the length and then terminate it with \0. void messageHandler(char *topic, byte *payload, unsigned int ...
hcheung's user avatar
  • 1,362
2 votes
Accepted

strcmp doesn't seem to work

You seem to already understand that it's not null-terminated and that's why it's not working the way you expect. So, I'll take a different tact for "how to fix it". You can simply not treat ...
timemage's user avatar
  • 5,163
2 votes

Read Comma Separated String

The AVR implementation of sscanf() does not support parsing floating point numbers, thus you only get zeros out of it. You can't use the %f specifier with it. You need to convert these numbers by ...
chrisl's user avatar
  • 16.2k
2 votes
Accepted

Arduino - Function -> return Cstr instead of string

You cannot work with c-strings like with Strings. You need to use the standard c-string functions (like sprintf(), strcpy() and siblings) or directly access the char array. This gives the following ...
chrisl's user avatar
  • 16.2k
2 votes

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

As you already got good answers to most of your questions, I will try to cover here only this one: Is it possible to quantify the fragmentation and get a feel of what's safe and when it becomes ...
Edgar Bonet's user avatar
  • 42.8k
2 votes
Accepted

String() vs char for simple flow control

Yes, String has a bit more overhead than plain c-strings (aka char arrays), though the additional execution time will be way too small for you to notice (especially when you type the commands in by ...
chrisl's user avatar
  • 16.2k
2 votes

Parse char* mac-string to uint8_t array

Personally I'd go with a basic implementation such as this one. const uint8_t MAC_length = 6; uint8_t* MAC_buffer[6]; uint8_t fromHexChar(char c) { uint8_t result = 0 if ((c >= '0') &&...
frarugi87's user avatar
  • 2,721
1 vote
Accepted

Reading bits from a const char pointer

You wrote: I wrote out a function to allow me to grab a piece of the string 32 bits at a time and convert that into a number while using an offset This function is almost correct. Try this: ...
Edgar Bonet's user avatar
  • 42.8k
1 vote

Arduino - Function -> return Cstr instead of string

One way is passing external buffer to readLine() to fill: size_t readLine(char * buff, size_t maxChars) { size_t pos = 0; // current position in buffer while (file.myFileAvalable()) { buff[pos]...
KIIV's user avatar
  • 4,742
1 vote

Any explanation for this behavior?

The problem is these lines of code in your function fr_fddb(): Firebase.get(firebaseData, full_path); _dt = firebaseData.cstrData(); You didn't share enough code to make this clear, but ...
romkey's user avatar
  • 1,544
1 vote
Accepted

Mqtt not subscribe on topic that's converted from String to char array

The c_str() function of the String class returns a pointer to current internal char array. This pointer is valid only until the String exists and is not changed. Variable espInTopicStr is defined in ...
Juraj's user avatar
  • 17.9k

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