Arduino data type conversion (single chip) detailed introduction _android

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.