我有这两个变量uint8_t* data_chars
和unsigned int length
。
data_chars
是指向字符数组的指针。 length
是字符数。
我想将其转换为Arduino中使用的String对象。
答案 0 :(得分:1)
好吧,因为缓冲区及其大小没有构造函数,所以你必须自己动手:
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
}
然而,这有点浪费记忆。
顺便说一句:如果你使用过char * data_chars
,即使没有类型转换,它也会正常工作。
答案 1 :(得分:0)
KIIV的答案几乎是正确的。但是,我相信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_chars[]
强制转换为char才能完全确定。来自KIIV答案的微小修改。信用仍归KIIV。