Arduino Data type Conversion
How to easily play Arduino SCM, when I was in the data conversion, encountered a problem, try C language and C + + many functions, did not achieve the float data conversion to char type. After hard access to the data, finally found a great god-level function!!! DTOSTRF (), you can easily implement data types from float to char. Get it
The format is as follows:
char* DTOSTRF (double _val,signed char _width, unsigned char prec, char* _s)
Parameter description:
_val: float or Double value to convert.
_width: The integer part length after conversion.
_prec: The length of the decimal part after conversion.
_s: Save to the char array.
Examples are as follows:
float f=3.1415;
Char c[];
DTOSTRF (f,1,2,c);
Serial.println (c);
At this time from the Arduino from the serial monitor will output 3.14, that is, the successful implementation of data type conversion.
After data conversion, we can make a concatenation of the strings. as follows, the concatenation of strings will be implemented
float f=3.1415;
Char c[];
DTOSTRF (f,1,2,c);
Serial.println (c);
Defines a string
char s[]= "///sensor1///ly///sensor1///pm2.5///100.70///";
The string concatenation operation
strcat (s,c);
Serial.println (s);
At this time will be in the Arduino with a serial monitor in the output///sensor1///ly///sensor1///pm2.5///100.70///3.14, that is, the successful implementation of the string stitching.
Special Note: Arduino can directly call the C language in the strcat () function to achieve the concatenation of strings.
Thank you for reading, I hope to help you, thank you for your support for this site!