user91579631 user91579631 - 1 year ago 49
C++ Question

Convert uint8_t* data_chars and length into String equivalent

I have these 2 variables

uint8_t* data_chars
unsigned int length

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

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

Answer Source

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.