Arduino data type conversion
How to easily play with Arduino microcontroller? When I was performing data conversion, I encountered a problem and tried many functions in C language and C++, but none of them achieved the purpose of converting float-type data into char-type. After searching the information hard, I finally found a great god-level function! ! ! dtostrf() can easily implement data types from float to char .
The format is as follows:
char* dtostrf(double _val,signed char _width, unsigned char prec, char* _s)
Parameter description:
_val: The float or double value to be converted.
_width: The length of the integer part after conversion.
_prec: The length of the fractional part after conversion.
_s:Save to this char array.
Examples are as follows:
float f=3.1415; char c[]; dtostrf(f,1,2,c); (c);
At this time, 3.14 will be output from the Arduino's own serial port monitor, which means that the data type conversion is successfully implemented.
After the data is converted, we can splice the strings. As follows, string stitching will be implemented
float f=3.1415; char c[]; dtostrf(f,1,2,c); (c); //Define a stringchar s[]="///sensor1///ly///sensor1///PM2.5///100.70///"; //Perform string splicing operationstrcat(s,c); (s);
At this time, ///sensor1///ly///sensor1////PM2.5///100.70////3.14 will be output in the Arduino's own serial port monitor, which will successfully realize the string splicing.
Special note: In Arduino, you can directly call the strcat() function in C language to realize the splicing of strings.
Thank you for reading, I hope it can help you. Thank you for your support for this site!