atod, atoi,atoboo,atoint64源码实现

本文详细介绍了四个用于将字符串转换为不同数值类型的函数:atoint将字符串转换为int32,atodouble转换为double,atobool转换为bool,atoint64转换为int64_t。这些函数通过解析字符串中的数字、符号和小数点,实现了数值类型的精确转换。在处理数字字符串时,这些函数能有效地跳过空白字符并处理正负号。
摘要由CSDN通过智能技术生成

atoint 字符串转int32

int atoint(const char *nptr)
{
	int c;	   /* current char */
	int total; /* current total */
	int sign;  /* if '-', then negative, otherwise positive */

	/* skip whitespace */
	while (isspace((int)(unsigned char)*nptr))
		++nptr;

	c = (int)(unsigned char)*nptr++;
	sign = c; /* save sign indication */
	if (c == '-' || c == '+')
		c = (int)(unsigned char)*nptr++; /* skip sign */

	total = 0;

	while (isdigit(c))
	{
		total = 10 * total + (c - '0');	 /* accumulate digit */
		c = (int)(unsigned char)*nptr++; /* get next char */
	}

	if (sign == '-')
		return -total;
	else
		return total; /* return result, negated if necessary */
}

atodouble 字符串转double

double atodouble(const char *s)
{

	char **endptr = 0;
	register const char *p = s;
	register long double value = 0.L;
	int sign = 0;
	long double factor;
	unsigned int expo;

	while (isspace(*p))
		p++;

	if (*p == '-' || *p == '+')
		sign = *p++;

	while ((unsigned int)(*p - '0') < 10u)
		value = value * 10 + (*p++ - '0');

	if (*p == '.')
	{
		factor = 1.;
		p++;
		while ((unsigned int)(*p - '0') < 10u)

		{

			factor *= 0.1;

			value += (*p++ - '0') * factor;
		}
	}

	if ((*p | 32) == 'e')
	{

		expo = 0;
		factor = 10.L;

		switch (*++p)
		{

		case '-':

			factor = 0.1;

		case '+':

			p++;

			break;

		case '0':

		case '1':

		case '2':

		case '3':

		case '4':

		case '5':

		case '6':

		case '7':

		case '8':

		case '9':

			break;

		default:

			value = 0.L;
			p = s;
			goto done;
		}

		while ((unsigned int)(*p - '0') < 10u)
			expo = 10 * expo + (*p++ - '0');

		while (1)
		{
			if (expo & 1)
				value *= factor;

			if ((expo >>= 1) == 0)
				break;

			factor *= factor;
		}
	}

done:

	if (endptr != 0)
		*endptr = (char *)p;

	return (sign == '-' ? -value : value);
}

atobool 字符串转bool类型

	if (strcmp(nptr, "true") == 0)
	{
		return true;
	}
	else
	{
		return false;
	}

atoint64 字符串转int64

int64_t atoint64(const char *p)
{
	int64_t n;
	int c, neg = 0;
	unsigned char   *up = (unsigned char *)p;
 
	if (!isdigit(c = *up)) {
		while (isspace(c))
			c = *++up;
		switch (c) {
		case '-':
			neg++;
			/* FALLTHROUGH */
		case '+':
			c = *++up;
		}
		if (!isdigit(c))
			return (0);
	}
 
	for (n = '0' - c; isdigit(c = *++up); ) {
		n *= 10; /* two steps to avoid unnecessary overflow */
		n += '0' - c; /* accum neg to avoid surprises at MAX */
	}
 
	return (neg ? n : -n);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个较为详细的版本的atoi函数源码示例: ```c #include <stdio.h> #include <stdlib.h> #include <ctype.h> int atoi(const char *str) { int num = 0; int sign = 1; int i = 0; // 处理空白字符 while (isspace(str[i])) i++; // 处理正负号 if (str[i] == '-' || str[i] == '+') { sign = (str[i] == '-') ? -1 : 1; i++; } // 转换数字 while (isdigit(str[i])) { if (num > INT_MAX / 10 || (num == INT_MAX / 10 && (str[i] - '0') > INT_MAX % 10)) { // 处理溢出情况 if (sign == 1) return INT_MAX; else return INT_MIN; } num = num * 10 + (str[i] - '0'); i++; } return num * sign; } int main() { char str[] = "12345"; // 要转换的字符串 int num = atoi(str); // 使用自定义的atoi函数将字符串转换为整数 printf("转换后的整数为: %d\n", num); return 0; } ``` 这个版本的atoi函数与之前的简化版相比,增加了以下功能: - 使用`isspace`函数处理空白字符,而不仅仅是处理空格和制表符。 - 增加了溢出检查,以避免将超出整数范围的字符串转换为整数。如果溢出,则根据符号位返回INT_MAX或INT_MIN。 在上述示例中,我们首先包含了`stdlib.h`、`stdio.h`和`ctype.h`头文件。然后,我们定义了一个自定义的atoi函数,它接受一个指向const char类型的字符串,并返回相应的整数。 最后,我们在main函数中使用自定义的atoi函数将字符串转换为整数,并打印出转换后的结果。 请注意,这只是一个较为详细的版本的atoi函数示例。实际的atoi函数可能会更复杂,考虑更多的边界条件和错误处理情况。此外,该实现还未考虑负号后面跟随非数字字符的情况,如果你需要更严格的实现,请根据具体需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值