c++ - 将 uint8_t* data_chars 和长度转换为等效的字符串

标签 c++ string arduino

我有这 2 个变量 uint8_t* data_charsunsigned 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/

相关文章:

c++ - 具有大小的 vector 的类内初始化

java - 使用new创建字符串对象及其与intern方法的比较

我可以从传感器读取值,然后将其添加到 RF24l01 网络的 JSON 字符串中吗

C++ - 创建安全的 const char* 构造函数

c++ - 为什么我在这段代码上得到 "no match for call"?

c++ - 如何防止 strncpy_s 在调试版本中填充目标缓冲区?

c++ - Arduino UDP 错误 WiFiUdp.cpp

c++ - 在函数中填充指针数组

c++ - 发送 WM_SETTEXT 时如何避免 EN_CHANGE 通知?

c# - 如何在 C# 中将字符串转换为 UTF-8?