我有这 2 个变量 uint8_t* data_chars
和 unsigned int length
。
data_chars
是指向字符数组的指针。 length
是字符数。
我想将其转换为 Arduino 中使用的字符串对象。
最佳答案
好吧,因为缓冲区及其大小没有构造函数,所以你必须自己做:
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
,即使没有类型转换,它也能正常工作。
关于c++ - 将 uint8_t* data_chars 和长度转换为等效的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39788834/