C++ 将uint8_t*数据_字符和长度转换为等效字符串

C++ 将uint8_t*数据_字符和长度转换为等效字符串,c++,string,arduino,C++,String,Arduino,我有两个变量uint8*data\u chars和unsigned int length data\u chars是指向字符数组的指针length是字符数 我想将其转换为Arduino中使用的String对象 因为缓冲区及其大小没有构造函数,所以您必须自己做: String data; data.reserve(length+1); // prepare space for the buffer and extra termination character '\0' for (int i =

我有两个变量
uint8*data\u chars
unsigned int length

data\u chars
是指向字符数组的指针
length
是字符数


我想将其转换为Arduino中使用的String对象

因为缓冲区及其大小没有构造函数,所以您必须自己做:

String data;
data.reserve(length+1); // prepare space for the buffer and extra termination character '\0'
for (int i = 0; i<length; ++i) {
    data += (char)data_chars[i]; // typecast because String takes uint8_t as something else than char
}
字符串数据;
数据保留(长度+1);//为缓冲区和额外终止字符“\0”准备空间

对于(inti=0;iKIIV的答案几乎是正确的。然而,我相信100%的正确答案将在下面

String data;
data.reserve(length+1); // prepare space for the buffer and extra termination character '\0'
for (int i = 0; i<length; ++i) {
    data += (char) data_chars[i];
}
字符串数据;
data.reserve(长度+1);//为缓冲区和额外的终止字符“\0”准备空间

对于(int i=0;我确认我切换了答案。wk_sg提供的答案经过一些测试后是正确的。理论上,你可以编辑我的答案。没有必要添加另一个只在一个类型上不同的答案。KIIV,你是正确的,我无法撤消我所做的。我将要求OP将你的答案标记为正确答案。对不起,a关于它。好吧。如果你不介意的话,我会这么做。基夫应该得到更多。