The basic operation of the string Arduino

The basic operation of the string Arduino

Numeric string conversion

Value to String

c # code

int i=10;
string s=i.ToString();

Arduino Code

char s[5];
int i=10;
itoa(i,s,20);

Arduino core difference is that the use of c syntax, no string object so there is no corresponding method. Define the string object can only be used

char s[25];
char *s;

In other words, the whole is equal to the string array of characters

Function itoa () prototype

char *itoa(int value, char *string, int radix);

Prototype Description: value: data to be converted. string: address of the destination string. radix: hexadecimal number after conversion, it may be a decimal, hexadecimal, etc.

Function: Convert an integer string.

//把整数123 打印成一个字符串保存在s 中。  、
sprintf(s, "%d", 123); //产生"123"

One of the most common applications sprintf print than the integer into a string, so, spritnf in most cases can replace itoa.

123 // print the integer into a string stored in s.

sprintf (s, "% d", 123); // generates "123"

You can specify the width of the lack of space if left:

sprintf (s, "% 8d% 8d", 123, 4567); // yields: "1234567"

Of course, it can also be left-aligned:

sprintf (s, "% -8d% 8d", 123, 4567); // yields: "1234567"

It can also be printed in hex:

sprintf (s, "% 8x", 4567); // lowercase hexadecimal 16, the width of which occupy eight positions, right justified

sprintf (s, "% -8X", 4568); // uppercase hexadecimal 16, the width of which occupy eight positions, Left

sprintf control string format since all kinds of things can be inserted, and finally they "concatenated" naturally even possible to

Of the strings, so that on many occasions may alternatively strcat, sprintf but can be connected to a plurality of strings (natural may be simultaneously

Insert something else in the middle of them, in short, very flexible). such as:

char* who = “I”;

char* whom = “China”;

sprintf(s, “%s love %s.”, who, whom); //产生:"I love China. "

发布了14 篇原创文章 · 获赞 1 · 访问量 6696

Guess you like

Origin blog.csdn.net/weixin_43833645/article/details/90451754