
1 /************************************************* 2 *函数功能:根据步长分割字符串 3 *参数str:待处理的字符串 4 *参数step:步长 5 *返回值:返回处理得到的字符串 6 *备注:调用需包含string.h 7 *************************************************/ 8 char *strsplit(char *str,short step) 9 { 10 char bstr[1024] = {0};//转换缓冲区 11 static char *str_ptr = NULL; 12 static short idx = 0; 13 14 if(str) {str_ptr = str;idx = 0;} 15 else if(!str_ptr) {return NULL;} 16 if(idx>strlen(str_ptr)) {return NULL;} 17 strncpy(bstr,str_ptr+idx,step); 18 idx += step; 19 20 return bstr; 21 } 22 23 /************************************************/ 24 //用例:按步长4分割字符串,并转换成ASCII字符输出 25 /************************************************/ 26 #include <stdio.h> 27 #include <string.h> 28 29 char *strsplit(char *str,short step) 30 { 31 char bstr[1024] = {0};//转换缓冲区 32 static char *str_ptr = NULL; 33 static short idx = 0; 34 35 if(str) {str_ptr = str;idx = 0;} 36 else if(!str_ptr) {return NULL;} 37 if(idx>strlen(str_ptr)) {return NULL;} 38 strncpy(bstr,str_ptr+idx,step); 39 idx += step; 40 41 return bstr; 42 } 43 44 int main() 45 { 46 char str[] = "00630065006E007400650072002C0061002C0031003200330034003500360023"; 47 char str_tmp[80] = {0}; 48 short idx = 0; 49 char *token; 50 51 printf("%s ",str); 52 53 token = strsplit(str,4); 54 while(token != NULL) 55 { 56 char ch[5]={0}; 57 sscanf(token,"%X",ch); 58 strcat(str_tmp,ch); 59 token = strsplit(NULL,4); 60 } 61 62 printf(" %s ",str_tmp); 63 return 0; 64 }

1 /************************************************* 2 *函数功能:使用newstr替换oldstr,若newstr为空,则表示删除 3 *参数str:待处理的字符串 4 *参数oldstr:旧字符串 5 *参数newstr:新字符串(可传NULL) 6 *返回值:返回处理得到的字符串 7 *备注:调用需包含string.h 8 *************************************************/ 9 char *strrpc(char *str,char *oldstr,char *newstr) 10 { 11 char bstr[1024] = {0};//转换缓冲区 12 int i = 0; 13 14 if(!str||!oldstr) return NULL; 15 for(i=0;i<strlen(str);i++) 16 { 17 if(!strncmp(str+i,oldstr,strlen(oldstr))) 18 {//查找目标字符串 19 if(newstr!=NULL) {strcat(bstr,newstr);}//newstr参数若为NULL,则表示删除字符串 20 i += strlen(oldstr)-1; 21 }else{ 22 strncat(bstr,str+i,1);//保存一字节进缓冲区 23 } 24 } 25 26 strcpy(str,bstr); 27 return bstr; 28 } 29 30 /************************************************/ 31 //用例:将FF0C替换成002C,并删除00 32 /************************************************/ 33 #include <stdio.h> 34 #include <string.h> 35 36 char *strrpc(char *str,char *oldstr,char *newstr) 37 { 38 char bstr[1024] = {0};//转换缓冲区 39 int i = 0; 40 41 if(!str||!oldstr) return NULL; 42 for(i=0;i<strlen(str);i++) 43 { 44 if(!strncmp(str+i,oldstr,strlen(oldstr))) 45 {//查找目标字符串 46 if(newstr!=NULL) {strcat(bstr,newstr);}//newstr参数若为NULL,则表示删除字符串 47 i += strlen(oldstr)-1; 48 }else{ 49 strncat(bstr,str+i,1);//保存一字节进缓冲区 50 } 51 } 52 53 strcpy(str,bstr); 54 return bstr; 55 } 56 57 int main() 58 { 59 char str[] = "00630065006E007400650072FF0C0061FF0C0031003200330034003500360023"; 60 char str_tmp[80] = {0}; 61 62 printf("%s ",str); 63 64 strrpc(str,"FF0C","002C"); 65 strrpc(str,"00",NULL); 66 67 printf(" %s ",str); 68 69 return 0; 70 }

1 char* strtrim(char str_arr[], short str_len) 2 { 3 char* ptr = NULL; 4 short loop = 0; 5 6 if(!str_arr||!str_len) {return NULL;} 7 8 ptr = (char*)str_arr + str_len; 9 while(isspace(*ptr)) 10 {//先从尾部剔除空白符 11 *ptr = '