C语言 自写atof、atoi等函数

    在遇到一些flash很小的芯片的情况下,用库函数中的atof、atoi等等一些函数时引用对应库函数后会比较占内存,只能是通过自写函数来达到功能

atoi

int64_t my_atoi(const char * str)
{
	float num;
	int64_t ret = 0;
	char  sign = 0;
	while (*str == '\0')
		str++;
	if (*str == '-')
	{
		sign = -1;
		str++;
	}
	else
	{
		sign = 1;
	}
	while ((*str >= '0') && (*str <= '9'))
	{
		ret = ret * 10 + (*str - '0');
		str++;
	}
	return ret * sign;
}

注:转换得到的数据类型根据自己需要及编译器修改即可
 

floatTochar

int floatTochar(float slope, char*buffer, int Decimal)  //浮点型数,存储的字符数组,小数位数
{
	int temp, count = 0, j, len = 0;
	if (slope < 0)//判断是否大于0
	{
		buffer[len++] = '-';
		slope = -slope;
	}

	temp = (int)slope;//取整数部分
	if (temp == 0)
	{
		count = 1;
	}
	else
	{
		for (count = 0; temp != 0; count++)//计算整数部分的位数
			temp /= 10;
	}

	temp = (int)slope;
	for (j = count + len; j > len; j--)//将整数部分转换成字符串型
	{
		buffer[j-1] = temp % 10 + 0x30;
		temp /= 10;
	}
	len += count;
	buffer[len++] = '.';
	slope -= (int)slope;
	for (j = 0; j < Decimal; j++)//将小数部分转换成字符串型
	{
		slope *= 10;
		buffer[len++] = (int)slope + 0x30;
		slope -= (int)slope;
	}
	buffer[len] = '\0';
	return len;
}

 

intTochar

int intTochar(int int_num, char * buffer)
{
	int temp, count = 0, j, len = 0;
	if (int_num < 0)//判断是否大于0
	{
		buffer[len++] = '-';
		int_num = -int_num;
	}

	temp = int_num;//取整数部分
	
	if (temp == 0)
	{
		count = 1;
	}
	else
	{
		for (count = 0; temp != 0; count++)//计算整数部分的位数
			temp /= 10;
	}
	
	temp = int_num;
	for (j = count + len; j > len; j--)//将整数部分转换成字符串型
	{
		buffer[j - 1] = temp % 10 + 0x30;
		temp /= 10;
	}
	len += count;
	buffer[len] = '\0';
	return len;
}

 

my_atof

float my_atof(char * str)
{
 char p;
 float num;
 float ret = 0;
 char  sign = 0;
 while (*str == '\0')
	 str++;
 if (*str == '-')
 {
	 sign = -1;
	 str++;
 }
 else
 {
	 sign = 1;
 }
 while ((*str >= '0') && (*str <= '9'))
 {
	 ret = ret * 10 + (*str - '0');
	 str++;
 }
 if (*str == '.')
 {
	 str++;
     p = 0;
	 while ((*str >= '0') && (*str <= '9'))
	 {   
		 p++;
		 ret = ret * 10 + (*str - '0');
		 str++;
	 }
	 num = pow(10.0f, p);
	 return (ret * sign) / num;
 } 
 else
	 return ret * sign;
}

 

my_memcpy

void my_memcpy(char *destin, char * source, unsigned char len)
{	
	while(len--)	
	{	
		*(destin++) = *(source++);
	}
}

 

my_memset

void my_memset(char *destin, char ch, unsigned char len)
{	
	while(len--)	
	{	
		*(destin++) = ch;
	}
}

 

my_strncmp

int my_strncmp ( char * s1, char * s2, unsigned char n) 
{
    if ( !n )//n为无符号整形变量;如果n为0,则返回0
    {
        return(0);
    }

    //在接下来的while函数中 

    //第一个循环条件:--n,如果比较到前n个字符则退出循环

    //第二个循环条件:*s1,如果s1指向的字符串末尾退出循环

    //第二个循环条件:*s1 == *s2,如果两字符比较不等则退出循环

    while (--n && *s1 && *s1 == *s2) 
    { 
        s1++;//S1指针自加1,指向下一个字符

        s2++;//S2指针自加1,指向下一个字符
    }
    return( *s1 - *s2 );//返回比较结果
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值