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

一个以'\0'结尾的11位字符串,都是数字字符,将其转化为Long型的数并返回 用VC作。

#include <string.h>
#include <stdio.h>
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;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-04-19
有现成的函数,atol

函数名: atol
功 能: 把字符串转换成长整型数
long l;
char *str = "98765432";
l = atol(str);本回答被网友采纳
第2个回答  2013-04-19
#include <stdio.h>

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

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