头文件:my_string.h

#ifndef __MY_STRING__
	#define __MY_STRING__
	/*十六进制数值字符串转整数*/
	int ch_to_hex(const char* val_in,int size);
	
	/*判断给定字符串是否是十六进制数*/
	int is_hex(const char* val,int size);
	
	/*判断给定字符串是否是数字*/
	/*R_V:1(数字) 0(非数字)*/
	int is_num(const char* val);
	
	/*字符转整数数字(对于是否是纯数字不做判断)*/
	int ch_to_num(const char* val);
	
	/*将英文字母小写->大写*/
	void to_upper( char* buf, int size);
	
	/*将英文字母大写->小写*/
	void to_lower( char* buf, int size);
	
	/*去掉字符串左边的空格*/
	/*R_V:-1 转换失败 转换后的字节长度*/
	int left_trim(char* buf,int size);
	
	/*去掉字符串右边的空格*/
	/*R_V:-1 转换失败 转换后的字节长度*/
	int right_trim(char* buf,int size);
	
	/*去掉字符串两边的空格*/
	/*R_V:-1 转换失败 转换后的字节长度*/
	int trim(char* buf, int size);
	
	/*将字符串内部连续多个空格缩减为1个空格*/
	void reduce_space(char* buf, int size);
	
	int split(const char* buf_in,int size_in,char* buf[],int size , char separator);
	
	void split_free(char* buf[],int size);
#endif

源代码:my_string.c

#include<string.h>

#include<malloc.h>

#include<memory.h>

/*判断给定字符串是否是十六进制数*/

int is_hex(const char* val,int size)

{

int status = 1;

char* buf_tmp = malloc(size+1);

memset(buf_tmp,0,size+1);

memcpy(buf_tmp,val,size);

to_upper(buf_tmp,size);

for(int index= 0 ; index < size; index++)

{

/*碰到x跳过去*/

if(index == 1 && buf_tmp[index]==0x58) continue;

if((buf_tmp[index]>0x39 && buf_tmp[index] <0x41) || (unsigned char)buf_tmp[index] <0x30 || (unsigned char)buf_tmp[index] > 0x46 )

{

status = 0;

break;

}

}

free(buf_tmp);

return status;

}

/*判断给定字符串是否是数字*/

/*R_V:1(数字) 0(非数字)*/

int is_num(const char* val)

{

int status = 1;

int index = 0;

while(val[index])

{

if(val[index]>0x39 || val[index] <0x30)

{

/*非数字*/

status =  0;

break;

}

index++;

}

return status;

}

/*十六进制数值字符串转整数*/

int ch_to_hex(const char* val_in,int size)

{

int result = 0;

int bit_pos = 0;

char* val = malloc(size+1);

memset(val,0,size+1);

memcpy(val,val_in,size);

to_upper(val,size);

for(int index = size-1;index>=0;index--)

{

/*碰到x跳出去*/

if(index == 1 && val[index]==0x58) break;

if(val[index]>= 0x30 && val[index] <=0x39)

{

result = ((val[index] - 0x30) << (bit_pos++ * 4)) | result;

}

else

{

result = ((val[index] - 0x41 + 10) << (bit_pos++ * 4)) | result;

}

}

free(val);

return result;

}

/*字符转整数数字(对于是否是纯数字不做判断)*/

int ch_to_num(const char* val)

{

int index = 0;

/*标示是否为负数*/

int i_negative = 0;

/*判断字符串中的数字是否有负号*/

if(val[index] == '-')

{

i_negative = 1;

index++;

}

int result = val[index++] - 0x30;

while(val[index])

{

/*能进循环,说明不止一位数*/

result = result * 10;

result +=  (val[index++] - 0x30);

}

if(i_negative ==  1) result = 0 - result;

return result;

}

/*将英文字母小写->大写*/

void to_upper( char* buf, int size)

{

if(!buf || size <= 0 ) return;

for(int index = 0 ; index < size; index++)

{

if(buf[index] >= 0x61 && buf[index] <= 0x7A)

{

buf[index] = buf[index] - 0x20;

}

}

}

/*将英文字母大写->小写*/

void to_lower( char* buf, int size)

{

if(!buf || size <= 0 ) return;

for(int index = 0 ; index < size; index++)

{

if(buf[index] >= 0x41 && buf[index] <= 0x5A)

{

buf[index] = buf[index] + 0x20;

}

}

}

/*去掉字符串左边的空格*/

/*R_V:-1 转换失败 转换后的字节长度*/

int left_trim(char* buf,int size)

{

if(!buf || size <= 0 ) return -1;

char* buf_tmp = malloc(size + 1);

memset(buf_tmp,0,size + 1);

int index = 0;

for( index = 0 ; index < size; index++)

{

if(buf[index] != 0x20) break;

}

memcpy(buf_tmp,buf+index,size - index);

strcpy(buf,buf_tmp);

free(buf_tmp);

return size - index;

}

/*去掉字符串右边的空格*/

/*R_V:-1 转换失败 转换后的字节长度*/

int right_trim(char* buf,int size)

{

if(!buf || size <= 0 ) return -1;

char* buf_tmp = malloc(size + 1);

memset(buf_tmp,0,size + 1);


int index = 0;

for(index = size - 1;index >= 0 ; index--)

{

if(buf[index] != 0x20) break;

}

memcpy(buf_tmp,buf,index+1);

strcpy(buf,buf_tmp);

free(buf_tmp);

return index+1;

}

/*去掉字符串两边的空格*/

/*R_V:-1 转换失败 转换后的字节长度*/

int trim(char* buf, int size)

{

int result = 0;

if((result= left_trim(buf,size)) == -1) return -1;

if((result= right_trim(buf,result)) == -1) return -1;

return result;

}

/*将字符串内部连续多个空格缩减为1个空格*/

void reduce_space(char* buf, int size)

{

int index = 0;

int index_tmp = 0;

int flag = 0 ;

char* buf_tmp = malloc(size+1);

memset(buf_tmp,0,size+1);

for(index =0;index<size;index++)

{

if(buf[index] != 0x20)

{

if(flag == 1) 

{

buf_tmp[index_tmp++] = 0x20;

flag = 0;

}

buf_tmp[index_tmp++] = buf[index];

}

else

{

flag = 1;

}

}

memset(buf,0,size);

memcpy(buf,buf_tmp,index_tmp);

free(buf_tmp);

return;

}

int split(const char* buf_in,int size_in,char* buf[],int size , char separator)

{

int index = 0 ;

int count = 0 ;

int flag = 0;

for(index = 0;index < size_in; index++)

{

if(buf_in[index] == separator)

{

if(count == size) return count;

buf[count] = malloc(index - flag  + 1);

memset(buf[count],0,index-flag+1);

memcpy(buf[count++],&buf_in[flag],index-flag);

flag = index + 1;

}

}

buf[count] = malloc(index - flag  + 1);

memset(buf[count],0,index-flag+1);

memcpy(buf[count++],&buf_in[flag],index-flag);

return count;

}

void split_free(char* buf[],int size)

{

for(int index = 0; index < size; index++)

{

free(buf[index]);

}

}