Can anyone recommend any online references for strings and string manipulation using ESP-IDF? I'm currently deploying the example ESP-IDF tcp_client program to my ESP32 using the following definitions in the example:
#include <string.h>
…
static const char *payload = "Message from ESP32 ";
…
int err = send(sock, payload, strlen(payload), 0);
and trying to figure out if it's possible to create strings (through concatenations, or += operators) and possibly send these newly created strings (or typecast to chars) across/over the socket.
Thanks for any comments and help.
Strings and String Manipulation
Re: Strings and String Manipulation
Use the normal C language string manipulation functions: https://en.wikibooks.org/wiki/C_Program ... nipulation. All the standard C functions should be available under ESP-IDF. If you want to use operators like '+' or '+=' with strings you need to convert your code to the C++ language.
Re: Strings and String Manipulation
Thanks for the reply markkuk.
I had much better luck today with the strncat() and strcpy() functions. I did run into another problem that I'm hoping you can help me with. I am trying to send bytes over TCP using the socket send() function. I've noticed a problem when trying to send a byte with a value of 0. Here is an example of my code:
This code produces the following result:
I believe the integer 0 is a null character that has no character to add to the sequence, even though it should just append a byte whose bits are all zero.
Do you see what I might be missing in order to append a byte with a value of 0? From the documentation you shared, I can see that "a null byte and bytes that follow it are not appended" - this seems to be a common string manipulation issue/feature mentioned for the functions on that link. Anyway, I'm wondering if I'm just not handling this concatenation of bits in the best way in general?
Again, thank you for any comments and any help.
I had much better luck today with the strncat() and strcpy() functions. I did run into another problem that I'm hoping you can help me with. I am trying to send bytes over TCP using the socket send() function. I've noticed a problem when trying to send a byte with a value of 0. Here is an example of my code:
This code produces the following result:
I believe the integer 0 is a null character that has no character to add to the sequence, even though it should just append a byte whose bits are all zero.
Do you see what I might be missing in order to append a byte with a value of 0? From the documentation you shared, I can see that "a null byte and bytes that follow it are not appended" - this seems to be a common string manipulation issue/feature mentioned for the functions on that link. Anyway, I'm wondering if I'm just not handling this concatenation of bits in the best way in general?
Again, thank you for any comments and any help.
Re: Strings and String Manipulation
The null byte is used as a string terminator by all the string handling functions. You can't use string functions for byte arrays containing general binary data, they are intended for human-readable text strings only. Use the memcpy() and memmove() functions to copy blocks of bytes, or regular assignment to set individual bytes.