strtol,strtoll,strtoul, strtoull字符串转化成数字

一、功能:
这一组函数根据指定的进制base(2-36或者0,为0时根据字符串自动推断转换的进制)将nptr指向的字符串转换为对应的整形。
nptr可以以多个空格(isspace函数返回true的字符)开头,接着也可以有'+'或者'-'正负符号标记。如果base是16或者是0,接下来可以有“0x”的前缀,此时字符串将会当作16进制数字进行转换;如果接下的字符是‘0’,将会被当作8进制进行转换;除此之外都会被当作默认的十进制。
除了前缀之外其余的字符串将会被转换为对应的整形,并且会在给定进制base的第一个非有效数字字符终止(对于10以上的进制,'A'表示10,‘11’表示B,以此类推,'Z'表示35)。
如果endptr是非空,则endptr将会保存nptr中第一个指定进制base的无效字符串的地址;如果不存在有效字符串,则endptr会保存原始nptr的地址,并且返回0。通常情况,如果*nptr值不为'\0',返回时**endptr值为'\0',整个nptr字符串都是无效的。

二、声明
#include <stdlib.h>
long int strtol(const char *nptr, char **endptr, int base);
long long int strtoll(const char *nptr, char **endptr, int base);
unsigned long int strtoul(const char *nptr, char **endptr, int base);
unsigned long long int strtoull(const char *nptr, char **endptr, int base);
三、返回值:
返回对应的转换结果。对于结果溢出的现象,分别返回对应的整形的最大值,或者最小值(LONG_MAX LONG_MIN LLONG_MAX LLONG_MIN ULONG_MAX ULLONG_MAX ULLONG_MAX),此时errno被置为ERANGE.对于转换无符号整形的几个函数,如果字符串有前置的减号标记,转换的结果仍会被转换无无符号整形。
ERRORS:
EINVAL base值不支持
ERANGE 结果溢出
有的实现在转换没有进行时(没有数字字符输入,返回0),也可能将errno设置为EINVAL。
注意:
因为这一组函数,在成功或者失败的情况下,都有可能合理的返回0或者溢出极值,调用者在使用之前应该将errno设置为0,并且在调用之后检查errno的值来确定是否有错误发生。
按照上面的文档对几个函数进行封装,便于在实践中使用,具体如下:
 

bool stringToI32(const std::string &str, int32_t  &val)
{
	long temp_val = 0;
	bool isOK = stringToL(str, temp_val);
	val = temp_val;
	return isOK && (temp_val >= -0x7fffffff && temp_val <= 0x7fffffff/*32bit整形的有效范围*/);
}
 
bool stringToU32(const std::string &str, uint32_t  &val)
{
	unsigned long temp_val = 0;
	bool isOK = stringToUL(str, temp_val);
	val = temp_val;
	return isOK && (temp_val <= 0xffffffff/*32bit无符号整形的有效范围*/);
}
 
bool stringToI64(const std::string &str, int64_t  &val)
{
	long long temp_val = 0;
	bool isOK = stringToLL(str, temp_val);
	val = temp_val;
	return isOK && (temp_val >= -0x7fffffffffffffff && temp_val <= 0x7fffffffffffffff/*64bit整形的有效范围*/);
}
 
bool stringToU64(const std::string &str, uint64_t  &val)
{
	unsigned long long temp_val = 0;
	bool isOK = stringToULL(str, temp_val);
	val = temp_val;
	return isOK && (temp_val <= 0xffffffffffffffff/*64bit无符号整形的有效范围*/);
}
 
bool stringToL(const std::string &str, long  &val)
{
	bool isOK = false;
	const char *nptr = str.c_str();
	char *endptr = NULL;
	errno = 0;
	val = strtol(nptr, &endptr, 10);
	//error ocur
	if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
		|| (errno != 0 && val == 0))
	{
 
	}
	//no digit find
	else if (endptr == nptr)
	{
 
	}
	else if (*endptr != '\0')
	{
		// printf("Further characters after number: %s\n", endptr);
	}
	else
	{
		isOK = true;
	}
 
	return isOK;
}
 
bool stringToLL(const std::string &str, long long &val)
{
	bool isOK = false;
	const char *nptr = str.c_str();
	char *endptr = NULL;
	errno = 0;
	val = strtoll(nptr, &endptr, 10);
	//error ocur
	if ((errno == ERANGE && (val == LLONG_MAX || val == LLONG_MIN))
		|| (errno != 0 && val == 0))
	{
 
	}
	//no digit find
	else if (endptr == nptr)
	{
 
	}
	else if (*endptr != '\0')
	{
		// printf("Further characters after number: %s\n", endptr);
	}
	else
	{
		isOK = true;
	}
 
	return isOK;
}
 
bool stringToUL(const std::string &str, unsigned long  &val)
{
	bool isOK = false;
	const char *nptr = str.c_str();
	char *endptr = NULL;
	errno = 0;
	val = strtoul(nptr, &endptr, 10);
	//error ocur
	if ((errno == ERANGE && (val == ULONG_MAX))
		|| (errno != 0 && val == 0))
	{
 
	}
	//no digit find
	else if (endptr == nptr)
	{
 
	}
	else if (*endptr != '\0')
	{
		// printf("Further characters after number: %s\n", endptr);
	}
	else
	{
		isOK = true;
	}
 
	return isOK;
}
 
bool stringToULL(const std::string &str, unsigned long long &val)
{
	bool isOK = false;
	const char *nptr = str.c_str();
	char *endptr = NULL;
	errno = 0;
	val = strtoull(nptr, &endptr, 10);
	//error ocur
	if ((errno == ERANGE && (val == ULLONG_MAX))
		|| (errno != 0 && val == 0))
	{
 
	}
	//no digit find
	else if (endptr == nptr)
	{
 
	}
	else if (*endptr != '\0')
	{
		// printf("Further characters after number: %s\n", endptr);
	}
	else
	{
		isOK = true;
	}
 
	return isOK;
}

猜你喜欢

转载自blog.csdn.net/qq_37050329/article/details/88968859