MCU C language string operation

Publisher:快乐阳光Latest update time:2015-02-06 Source: 51heiKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Convert string to int atoi
Function prototype int atoi(const char *nptr);
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.
ARDUINO CODE

 
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.

Keywords:MCU Reference address:MCU C language string operation

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

Design of optical power meter based on 51 single chip microcomputer
1 Background Overview 1.1 Definition of Optical Power Optical power is the work done by light per unit time. Common units of optical power are milliwatts (mW) and decibels (dB), where the relationship between the two is 1mW=0dB, and decibels less than 1mW are negative values. For example, in the manual of a fiber o
[Microcontroller]
Design of optical power meter based on 51 single chip microcomputer
Design of Single Chip Microcomputer Electronic Piano
1. Experimental tasks (1. A 4X4 matrix of 16 buttons is designed to produce 16 tones. (2. You can play the music you want to express. 2. Circuit diagram Figure 4.22.1 3. System board hardware connection (1. Connect the P1.0 port in the "MCU System" area to the SPK IN port in the "Audio Amplifier Module
[Microcontroller]
Design of Single Chip Microcomputer Electronic Piano
Analysis of the internal composition and application principle of 51 single chip microcomputer
1. MCU internal resources STC89C52: 8K FLASH, 512 bytes RAM, 32 IO ports, 3 timers, 1 UART, 8 interrupt sources (1) Flash (hard disk) - program storage space - erase and write 100,000 times, data will not be lost when power is off, slow reading and writing speed (2) RAM (memory) - data storage space - data is lost whe
[Microcontroller]
Analysis of the internal composition and application principle of 51 single chip microcomputer
Those anti-interference technologies of single-chip microcomputer application systems that cannot be ignored
With the development of single-chip microcomputers, single-chip microcomputers are increasingly used in the fields of household appliances, industrial automation, production process control, intelligent instruments and meters, etc. However, various electrical equipment in the same power system are closely connected to
[Microcontroller]
CC3220R Main Features Introduction Single-Chip Wireless ARM MCU Solution
TI's CC3220R is a SimpleLink™ Wi-Fi® and Internet of Things (IoT) single-chip wireless MCU platform, including Wi-Fi, Bluetooth® low energy, Sub-1 GHz and main MCU and a rich tool portfolio. The application processor ARM® Cortex®-M4 MCU (operating frequency 80MHz) has a user-dedicated 256KB RAM and an optional 1MB XIP
[Microcontroller]
CC3220R Main Features Introduction Single-Chip Wireless ARM MCU Solution
After using MCU for so many years, do you know which MCU manufacturers are the best?
The single-chip microcomputer was born in 1971, and has gone through three stages: SCM, MCU , and SoC. The single-chip microcomputer has developed from 1-bit, 4-bit, 8-bit, and 16-bit to the current 32-bit and even 64-bit.   With the rapid development of consumer electronic products after the 1990s, the technology of
[Microcontroller]
After using MCU for so many years, do you know which MCU manufacturers are the best?
Feitian Chengxin: Plans to increase capital of holding subsidiary by RMB 41.61 million to develop MCU product line
On November 1, Feitian Chengxin announced that it plans to increase the capital of its holding subsidiary Hongsi Electronics by RMB 41.6117 million. After the capital increase, the registered capital of Hongsi Electronics will increase from RMB 14.52 million to RMB 60 million. The capital increase will mainly be used
[Mobile phone portable]
Microcontroller that combines Hi-Fi, intelligence and USB multi-channel features
With the continuous integration of many technological breakthroughs and new streaming services, and the growing prosperity of the smart home and smart speaker markets, consumers' demand for audio is no longer limited to sound quality itself, but more about pursuing high-quality life experience and convenient smart i
[Embedded]
Microcontroller that combines Hi-Fi, intelligence and USB multi-channel features
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号