Strings and String Manipulation

Ziegler
Posts: 11
Joined: Sun May 05, 2019 4:34 am

Strings and String Manipulation

Postby Ziegler » Mon Sep 23, 2019 4:56 am

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.

markkuk
Posts: 38
Joined: Wed Mar 27, 2019 11:50 am

Re: Strings and String Manipulation

Postby markkuk » Mon Sep 23, 2019 10:52 am

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.

Ziegler
Posts: 11
Joined: Sun May 05, 2019 4:34 am

Re: Strings and String Manipulation

Postby Ziegler » Tue Sep 24, 2019 4:26 am

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:
Code.PNG
Code.PNG (26.75 KiB) Viewed 16166 times
This code produces the following result:
Result.PNG
Result.PNG (8.2 KiB) Viewed 16166 times
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.

markkuk
Posts: 38
Joined: Wed Mar 27, 2019 11:50 am

Re: Strings and String Manipulation

Postby markkuk » Tue Sep 24, 2019 10:49 am

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.

Who is online

Users browsing this forum: Baidu [Spider], Baldhead, igormoo and 122 guests