void setup()
{
Serial.begin(9600);
}
void loop()
{
unsigned long along = 1234213;
String str1 = String(along);
str1 += "mimi";
char cArr[str1.length() + 1];
char cArr2[str1.length() + 3];
str1.toCharArray(cArr,str1.length() + 1);
str1.toCharArray(cArr2,str1.length() + 3);
Serial.println(str1);
Serial.println(cArr);
Serial.println(cArr2);
Serial.println(sizeof(str1));
Serial.println(sizeof(cArr));
Serial.println(sizeof(cArr2));
Serial.println(strlen(cArr));
Serial.println(strlen(cArr2));
if(cArr[str1.length()] == '\0')
{
Serial.println("cArr1: has ending mark. ");
}
if(cArr2[str1.length()] == '\0')
{
Serial.println("cArr2: has ending mark. ");
}
while(1);
}
一般用于将数值转为字符串或字符数组保存起来或传给lcd.
.toCharArray(字符数组buf, 字符数组长度);