C⼀C++基本问题:字符串转化为long型数字

2024-11-15 11:24:18
推荐回答(3个)
回答1:

#include 
#include
int main( int argc, char** argv )
{
/** 字符串转数字 */
char* str = "123456";
/** atol is ascii to long.
* atof is ascii to float.
*/
long num = atol(str);
printf("String %s trans to number %ld.\n",str, num);
return 0;
}

回答2:

有现成的函数,atol

函数名: atol
功 能: 把字符串转换成长整型数
long l;
char *str = "98765432";
l = atol(str);

回答3:

#include

__int64 str2long( char* s )
{
__int64 n = 0;
while ( *s ) {
n *= 10;
n += *s - '0';
++s;
}
return n;
}

int main()
{
printf( "%I64d\n", str2long( "123456578912" ) );
}