第四十八课--C语言数据处理函数介绍/zh

来自Microduino Wikipedia
跳转至: 导航搜索

目的

本教程将给大家介绍字符串转int、 int转字符串,怎么替换字符这类的问题。其它的教程都偏向于硬件,对C语言的熟知不够,现在就给新同学列出几个常用的数据处理、转换的函数,纯C的东西,这些看看书就能学会的,很好找资料,C语言是基础。

设备

  • 其他硬件设备


字符串转int atoi

函数原型int atoi(const char *nptr);

int val = atoi("12");

这样va就l等于12

	// the setup function runs once when you press reset or power the board
	void setup() {
	    // initialize serial communication at 9600 bits per second:
	  Serial.begin(9600);
	  const char* sss="12";
	  int val = atoi(sss);
	  Serial.println(val);
	}

	// the loop function runs over and over again forever
	void loop() {
	}

int转字符串 itoa

函数原型:char*itoa(intvalue,char*string,intradix);

int number=12345;

char string[25];

itoa(number,string,10);

string就是12345 后面的10是说转化成10进制 16就是转化成16进制 8就是转化成8进制 ,也可以转化成2进制


	// the setup function runs once when you press reset or power the board
	void setup() {
	    // initialize serial communication at 9600 bits per second:
	  Serial.begin(9600);
	  
	  int number=12345;
	  char string[25];
	  itoa(number,string,10);
	  Serial.println(string);

	}

	// the loop function runs over and over again forever
	void loop() {
	}

字符串截取 strncpy

原型:char*strncpy(char*dest,char*src,size_tnum);

大家知道strcpy 是子环节复制字符串,可能不知道还有个strncpy,这是复制指定长度的字符串

chardes[]="Hello,iam!";

charsource[]="abcdefg";

strncpy(chardes,charsource,3);

这时chardes的值是abc 注意这里可不是把abc添加在!后面的,是从chardes这个数组的首地址开始赋值的,最后会加上'\0'做结束符。

改变一下用法

strncpy(chardes+1,charsource+2,3);

这时候chardes的值是Hcde 为什么是这个值,能看懂不?自己思考一下。偏移哈。

	char* chardes="Hello,iam!";

	char* charsource="abcdefg";

	// the setup function runs once when you press reset or power the board
	void setup() {
	    // initialize serial communication at 9600 bits per second:
	  Serial.begin(9600);

	  strncpy(chardes,charsource,3); 
	  Serial.println(chardes);
	  
	  strncpy(chardes+1,charsource+2,3); 
	  Serial.println(chardes);
	}

	// the loop function runs over and over again forever
	void loop() {
	}

字符串构造 sprintf

原型 int sprintf( char *buffer, const char *format, [ argument] … );

这个应该很多人都知道。

char dest[20];

int val=12;

sprintf(dest,"val=%d",val);

dest的值就是"val=12" 那个12也是字符串了哈注意下。

这个函数也可以整形、浮点型转字符串

sprintf(dest,"%f",3.1415926f);

dest 就是“3.1415926”

sprintf(dest,"现在的时间是%s,请注意","2014-04-09 11:27:21"); 后面这个字符串也可以换成变量

dest的值是 "现在的时间是2014-04-09 11:27:21,请注意"

	// the setup function runs once when you press reset or power the board
	void setup() {
	    // initialize serial communication at 9600 bits per second:
	  Serial.begin(9600);

	  char dest[20];
	  char dest1[50];
	  char dest2[50];
	  
	  int val=12;
	  
	  sprintf(dest,"val=%d",val); 
	  Serial.println(dest);
	  
	  sprintf(dest1,"%f",3.1415926f); 
	  Serial.println(dest1);
	  
	  sprintf(dest2,"time now is %s, pay attention","2014-04-09 11:27:21");
	  Serial.println(dest2);
	}

	// the loop function runs over and over again forever
	void loop() {
	}

%f 会显示?

直接对内存操作memset

原型 void *memset(void *s, int ch, size_t n);

char buffer[20];

strcpy(buffer,"1234567890");

memset(buffer,0,sizeof(char)*20);

这时buffer中的数据全都是0了

strcpy(buffer,"1234567890");

memset(buffer+2,6,sizeof(char)*2);

这时buffer值是1266567890

这个函数可以对任何数据类型的内存进行修改。所以有些从串口接收进来的数据需要做一下简单的修改再转发出去的话就可以用这个函数做修改。

先介绍这些吧,再想起来别的再补充

memcpy函数使用起来也不错的,直接把字节数组复制到指定位置了,这个就不说了。

	// the setup function runs once when you press reset or power the board
	void setup() {
	    // initialize serial communication at 9600 bits per second:
	  Serial.begin(9600);

	  char buffer[20];

	  strcpy(buffer,"1234567890");
	  memset(buffer,0,sizeof(char)*20); 
	  Serial.println(buffer);
	  
	  strcpy(buffer,"1234567890");
	  memset(buffer+2,6,sizeof(char)*2); 
	  Serial.println(buffer);
	}

	// the loop function runs over and over again forever
	void loop() {
	}

字符串分割strtok

原型 char *strtok(char s[], const char *delim);

分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符串。

ARDUINO 代码复制打印

	// the setup function runs once when you press reset or power the board
	void setup() {
	    // initialize serial communication at 9600 bits per second:
	  Serial.begin(9600);

	    char input[16] = "abc,d";
	    char* p;
	    p = strtok(input, ",");
	    Serial.println(p);
	    
	    p = strtok(NULL, ",");
	    //if (p) printf("%s\n", p);
	    Serial.println(p);
	}

	// the loop function runs over and over again forever
	void loop() {
	}

输出: abc d

记住了,只要第一次分割的时候需要指定字符串,之后再分割就用NULL就行了,当p==NULL的时候说明分割完了。这里使用到了指针,此处指针不需要释放。

查找字符串 strstr

原型 char *strstr(const char *str1, const char *str2);

strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。

str1: 被查找目标 

str2: 要查找对象 

char str[]="1234 xyz";

char* str1=strstr(str,"34");

printf("%s",str1);

显示: 34 xyz

	// the setup function runs once when you press reset or power the board
	void setup() {
	    // initialize serial communication at 9600 bits per second:
	  Serial.begin(9600);

	char str[]="1234 xyz";

	char* str1=strstr(str,"34");
	Serial.println(str1);
	}

	// the loop function runs over and over again forever
	void loop() {
	}

字符串比较 strcmp

原型:extern int strcmp(const char *s1,const char * s2);

比较s1和s2 两个相等就返回0 不想等就返回非0值。

	// the setup function runs once when you press reset or power the board
	void setup() {
	    // initialize serial communication at 9600 bits per second:
	  Serial.begin(9600);

	  int equal;
	  equal=strcmp("eee","eee");
	    Serial.println(equal);
	}

	// the loop function runs over and over again forever
	void loop() {
	}

字符串连接strcat

原型 extern char *strcat(char *dest,char *src);

把src 连接到dest值的后面,注意dest要有足够的空间去接收src否则会出错

	// the setup function runs once when you press reset or power the board
	void setup() {
	    // initialize serial communication at 9600 bits per second:
	  Serial.begin(9600);

	//extern char *strcat(char *dest,char *src); 

	char* combin;

	  combin=strcat("lllll","eee");
	    Serial.println(combin);
	}

	// the loop function runs over and over again forever
	void loop() {
	}

程序

结果

视频