int val = atoi("12");
so va is equal to 12
int to string itoa
function prototype: char*itoa(intvalue,char*string,intradix);
int number=123456;
char string[25];
itoa(number,string,10);
string is 123456. The 10 at the end means it is converted to decimal. 16 means it is converted to hexadecimal. 8 means it is converted to octal. It can also be converted to binary.
String interception strncpy
prototype: char*strncpy(char*dest,char*src,size_tnum);
As we all know, strcpy is a sub-link to copy strings. You may not know that there is also strncpy, which copies a string of a specified length.
chardes[]="Hello,iam!";
charsource[]="abcdefg";
strncpy(chardes,charsource,3);
At this time, the value of chardes is abc Note that here, abc is not added after !. Instead, the value is assigned from the first address of the chardes array, and '\0' is added as the terminator.
Change the usage to
strncpy(chardes+1,charsource+2,3);
At this time, the value of chardes is Hcde. Why is it this value? Do you understand? Think about it yourself. Offset.
String construction sprintf
prototype int sprintf( char *buffer, const char *format, [ argument] … );
This should be known to many people.
char dest[20];
int val=12;
sprintf(dest,"val=%d",val);
The value of dest is "val=12". The 12 is also a string. Please note.
This function can also convert integer and floating point types to strings
sprintf(dest,"%f",3.1415926f);
dest is "3.1415926"
sprintf(dest,"The current time is %s, please note","2014-04-09 11:27:21"); The latter string can also be replaced by
the value of the variable dest, which is "The current time is 2014-04-09 11:27:21, please note"
Direct memory operation memset
prototype void *memset(void *s, int ch, size_t n);
char buffer[20];
strcpy(buffer,"1234567890");
memset(buffer,0,sizeof(char)*20);
At this time, the data in buffer is all 0
strcpy(buffer,"1234567890");
memset(buffer+2,6,sizeof(char)*2);
At this time, the buffer value is 1266567890.
This function can modify the memory of any data type. So if some data received from the serial port needs to be simply modified before being forwarded, this function can be used to modify it.
Let me introduce these first, and then I will add other things when I think of them
[2014-04-11 supplement]
The memcpy function is also good to use. It directly copies the byte array to the specified location, so I won’t talk about this.
String segmentation strtok
prototype char *strtok(char s[], const char *delim);
Decompose a string into a group of strings. s is the string to be decomposed, and delim is the delimiter string.
Output:
abc
d
Remember, you only need to specify a string when you split for the first time, and use NULL for subsequent splits. When p==NULL, it means the split is complete. Pointers are used here, and the pointer does not need to be released here.
Search string strstr
prototype char *strstr(const char *str1, const char *str2);
strstr() function searches for the first occurrence of a string in another string.
str1: target to be searched
str2: object to be searched
char str[]="1234 xyz";
char* str1=strstr(str,"34");
printf("%s",str1);
Display: 34 xyz
String comparison strcmp
prototype: extern int strcmp(const char *s1,const char * s2);
Compare s1 and s2. Return 0 if the two are equal. Return a non-zero value if they are not equal.
String concatenation strcat
prototype extern char *strcat(char *dest,char *src);
concatenate src to the end of dest value. Note that dest must have enough space to receive src, otherwise an error will occur.
Previous article:Summary of the problem that the microcontroller program cannot be downloaded
Next article:MCU Development Interrupts and Some Understanding
Recommended ReadingLatest update time:2024-11-16 21:37
- Popular Resources
- Popular amplifiers
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- Modern Electronic Technology Training Course (Edited by Yao Youfeng)
- Modern arc welding power supply and its control
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Statistics of domestic scientific laws
- DAC adjusts the output voltage of DCDC chip
- [CH549 Review] Part 3: Comparative Review of Low-Level Driver Software - SPI Interface LCD Driver
- Installing the NI-SCOPE Driver in Labview
- QT drawing problem
- German-made SDR radio PCB design
- Free Review: ST MEMS Waterproof Air Pressure Sensor LPS27HHW
- 3 levels and 4 misunderstandings of playing with microcontrollers
- Miniature Alphabet World Clock
- 51 MCU serial port connected to laser sensor to display distance