Convert Float to Char Then Concatenate It in Arduino

Have you tried converting float to char then concatenate it in your Arduino sketch? Many people on the Internet will suggest you to use sprintf C function like this:

float firstData = 70.55;
float secondData = 80.67;
char buff[20];
sprintf(buff, "data;%f;%f", firstData, secondData);

I’m sorry. It doesn’t work for float data type in Arduino environment. The output will look like this.

data;?;?

Instead of

data;70.55;80.67

I found the solution. It uses string data type like this:

float firstData = 70.55;
float secondData = 80.67;
 
String strData = "data;";
strData.concat(firstData);
strData.concat(";");
strData.concat(secondData);
 
char finalData[20];
strData.toCharArray(finalData, 20);
 
// then we can use the finalData
Serial.println(finalData);
稿源:Asep Bagja Priandana (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合编程 » Convert Float to Char Then Concatenate It in Arduino

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录