c++ 中如何把string转为int类型

 我来答
tczzya
2006-06-03 · TA获得超过129个赞
知道小有建树答主
回答量:216
采纳率:100%
帮助的人:94.2万
展开全部
用atoi这个函数,下面的来自msdn
atof, atoi, _atoi64, atol
Convert strings to double (atof), integer (atoi, _atoi64), or long (atol).

double atof( const char *string );

int atoi( const char *string );

__int64 _atoi64( const char *string );

long atol( const char *string );

Routine Required Header Compatibility
atof <math.h> and <stdlib.h> ANSI, Win 95, Win NT
atoi <stdlib.h> ANSI, Win 95, Win NT
_atoi64 <stdlib.h> Win 95, Win NT
atol <stdlib.h> ANSI, Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

Each function returns the double, int, __int64 or long value produced by interpreting the input characters as a number. The return value is 0 (for atoi and _atoi64), 0L (for atol), or 0.0 (for atof) if the input cannot be converted to a value of that type. The return value is undefined in case of overflow.

Parameter

string

String to be converted

Remarks

These functions convert a character string to a double-precision floating-point value (atof), an integer value (atoi and _atoi64), or a long integer value (atol). The input string is a sequence of characters that can be interpreted as a numerical value of the specified type. The output value is affected by the setting of the LC_NUMERIC category in the current locale. For more information on the LC_NUMERIC category, see setlocale. The longest string size that atof can handle is 100 characters. The function stops reading the input string at the first character that it cannot recognize as part of a number. This character may be the null character ('\0') terminating the string.

The string argument to atof has the following form:

[whitespace] [sign] [digits] [.digits] [ {d | D | e | E }[sign]digits]

A whitespace consists of space and/or tab characters, which are ignored; sign is either plus (+) or minus ( – ); and digits are one or more decimal digits. If no digits appear before the decimal point, at least one must appear after the decimal point. The decimal digits may be followed by an exponent, which consists of an introductory letter ( d, D, e, or E) and an optionally signed decimal integer.

atoi, _atoi64, and atol do not recognize decimal points or exponents. The string argument for these functions has the form:

[whitespace] [sign]digits

where whitespace, sign, and digits are exactly as described above for atof.

Generic-Text Routine Mappings

TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_ttoi atoi atoi _wtoi
_ttol atol atol _wtol

Example

/* ATOF.C: This program shows how numbers stored
* as strings can be converted to numeric values
* using the atof, atoi, and atol functions.
*/

#include <stdlib.h>
#include <stdio.h>

void main( void )
{
char *s; double x; int i; long l;

s = " -2309.12E-15"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );

s = "7.8912654773d210"; /* Test of atof */
x = atof( s );
printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );

s = " -9885 pigs"; /* Test of atoi */
i = atoi( s );
printf( "atoi test: ASCII string: %s\t\tinteger: %d\n", s, i );

s = "98854 dollars"; /* Test of atol */
l = atol( s );
printf( "atol test: ASCII string: %s\t\tlong: %ld\n", s, l );
}

Output

atof test: ASCII string: -2309.12E-15 float: -2.309120e-012
atof test: ASCII string: 7.8912654773d210 float: 7.891265e+210
atoi test: ASCII string: -9885 pigs integer: -9885
atol test: ASCII string: 98854 dollars long: 98854

Data Conversion Routines | Floating-Point Support Routines | Locale Routines

See Also _ecvt, _fcvt, _gcvt, setlocale, strtod, wcstol, strtoul
du瓶邪
2015-08-06 · TA获得超过2.4万个赞
知道大有可为答主
回答量:1.7万
采纳率:100%
帮助的人:2907万
展开全部
这个是纯代码实现的:
#include <iostream.h>
#include <windows.h>
bool convertstrtoint(char* src, int* dest);
int main(int argc, char* argv[])
{
int ret=0;
//输入范围:-2147483648 to 2147483647
if (convertstrtoint("-2147483648", &ret))
cout<<ret<<endl;
else
cout<<"error!"<<endl;
if (convertstrtoint("2147483647", &ret))
cout<<ret<<endl;
else
cout<<"error!"<<endl;
if (convertstrtoint("123456", &ret))
cout<<ret<<endl;
else
cout<<"error!"<<endl;
return 0;
}

bool convertstrtoint(char* src, int* dest)
{
char* temp=src; //用temp指针来操作字符串,保留原src值
*dest=0; //初始化结果
//int的取值范围是-2147483648 to 2147483647
int intsign=1; //符号,正数时值为1,负数时值为-1
if (*temp=='-') //判断字符串首位是否是负号{
intsign=-1; //字符串表示的是个负数
temp++; //指针加一指向表示数字的字符
}

if (*temp=='+') //判断字符串首位是否是正号
{
temp++; //指针加一指向表示数字的字符
}

/*******************************************/
/*下面判断输入的字符串是否超出了int表示范围*/
src=temp; //src指向字符串数值区,保存该位置

if (strlen(temp)>10)
return false; //字符串表示的数超出了int类型的范围
if (strlen(temp)==10)
{
//长度等于10时要判断是否超过了-2147483648 to 2147483647范围

//十亿位
if (*temp>'2')
return false;
else
if (*temp=='2')
{
//亿位
temp++;
if (*temp>'1')
return false;
else
if (*temp=='1')
{
//千万位
temp++;
if (*temp>'4')
return false;
else
if (*temp=='4')
{
//百万位
temp++;
if (*temp>'7')
return false;
else
if (*temp=='7')
{
//十万位
temp++;
if (*temp>'4')
return false;
else
if (*temp=='4')
{
//万位
temp++;
if (*temp>'8')
return false;
else
if (*temp=='8')
{
//千位
temp++;
if (*temp>'3')
return false;
else
if (*temp=='3')
{
//百位
temp++;
if (*temp>'6')
return false;
else
if (*temp=='6')
{
//十位
temp++;
if (*temp>'4')
return false;
else
if (*temp=='4')
{
//个位
temp++;
if (intsign==1)
{
//正数
if (*temp>'7')
return false;
}
else
{
//负数
if (*temp>'8')
return false;
}
}
}
}
}
}

/**************值范围判断完毕***************/
temp=src; //指针回到字符串数值开始的地方
/*******************************************/
int i=0; //循环控制变量
int multiple=0; //用来表示10的倍数
int tempnum;
while (strlen(temp))
{
multiple=1;
for (i=1; i<(int)strlen(temp); i++)
{
multiple*=10;
}

//temp指针所指的字符必须是一个字符表示的数字
if ((*temp>=0x30)&&(*temp<=0x39))
tempnum=*temp-0x30; //将字符表示的数字变成对应的数字
else
return false;

*dest+=tempnum*multiple; //输出指针当前所指字符表示的值
temp++; //指针移到下一个字符
}

//将结果加上对应的正负号
(*dest)*=intsign;

return true;
}
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
因没回帖被人砍
2006-06-03 · TA获得超过257个赞
知道小有建树答主
回答量:274
采纳率:17%
帮助的人:113万
展开全部
c不可以直接转换c++应该也不行吧``你转换来干什么?```
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式