user91579631 user91579631 - 2 months ago 6
C++ Question

Convert uint8_t* data_chars and length into String equivalent

I have these 2 variables

uint8_t* data_chars
and
unsigned int length
.

data_chars
is a pointer to an array of characters.
length
is the number of characters.

I would like to convert it to the String object used in Arduino.

Answer

Well, because there is no constructor for the buffer and its size, you have to do it yourself:

String data;
data.reserve(length+1); // prepare space for the buffer and extra termination character '\0'
for (int i = 0; i<length; ++i) {
    data += data_chars[i];
}

However it's kinda wasting a memory.