Arduino 字符转换错误?

Arduino 字符转换错误?,arduino,Arduino,我有这个: Serial.write(charBuf[0]); //gives 5 String data(charBuf); Serial.write(data); //gives nothing (space) 第一个是打印5,第二个什么也不打印。我错过了什么?这和以NULL结尾的东西有关吗 Serial.write(charBuf[0]); //gives 5 String data(charBuf); Serial.write(data); //gives no

我有这个:

 Serial.write(charBuf[0]);  //gives 5
 String data(charBuf);
 Serial.write(data);   //gives nothing (space)
第一个是打印5,第二个什么也不打印。我错过了什么?这和以NULL结尾的东西有关吗

 Serial.write(charBuf[0]);  //gives 5
 String data(charBuf);
 Serial.write(data);   //gives nothing (space)
以下是创建缓冲区的方式:

 Serial.write(charBuf[0]);  //gives 5
 String data(charBuf);
 Serial.write(data);   //gives nothing (space)
int len=1;
char charBuf[len];
for(int k=ACT_THRESH;k<ACT_THRESH+len;k++)
{
    charBuf[k-ACT_THRESH]= EEPROM.read(k);
}
int len=1;
char charBuf[len];
对于(int k=ACT_THRESH;k
int len=1;
int str_len=0;
char-charBuf[16];
对于(int k=ACT_THRESH;k

但是如果
len=1

你必须添加一个最终的
\0
才能得到一个有效的
字符串,我在哪里创建它?在我的示例中我是如何添加它的?这里len是1只是为了测试。顺便说一句,在你的回答中,你必须将len设置为2而不是1,我错了吗?哦,我明白了。但是为什么是16?你是如何得到一个大小的16字节?否
charBuf
大小是16,字符串的长度是
stru len
charBuf
在堆栈上,您可以使用您想要的大小,只需使用足够大的大小来存储数据。希望使用8倍的大小来进行正确对齐。如果您需要一个字符串,则需要为最后的
\0
提供额外的空间f length
len
您需要一个大小为
len+1
的缓冲区。我使用一个新的缓冲区,因为我不理解
len
的含义。另一个解决方案是使用
memset
例如,使用
0
填充缓冲区
 Serial.write(charBuf[0]);  //gives 5
 String data(charBuf);
 Serial.write(data);   //gives nothing (space)