CSTRING转换成int

liu7716640 2012-10-31 02:16:11
如何把cstring类型的字符串转换成可以用于计算的int类型
比功能如我现在
CString text_value;
先把text_value改成用于计算的int类型,求指教
...全文
200 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
yyps 2012-11-02
  • 打赏
  • 举报
回复
CString s(_T(“12”));
int n = atoi(s);

-->n = 12.
别说不行...
fujialin2011 2012-11-02
  • 打赏
  • 举报
回复
要学会使用msdn:
Convert a string to integer.


int atoi(
const char *str
);
int _wtoi(
const wchar_t *str
);
int _atoi_l(
const char *str,
_locale_t locale
);
int _wtoi_l(
const wchar_t *str,
_locale_t locale
);


Parameters
str
String to be converted.

locale
Locale to use.

Return Value
Each function returns the int value produced by interpreting the input characters as a number. The return value is 0 for atoi and _wtoi, if the input cannot be converted to a value of that type.

In Visual C++ 2005, in the case of overflow with large negative integral values, LONG_MIN is returned. atoi and _wtoi return INT_MAX and INT_MIN on these conditions. In all out-of-range cases, errno is set to ERANGE. If the parameter passed in is NULL, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, these functions set errno to EINVAL and return 0.

Remarks
These functions convert a character string to an integer value (atoi and _wtoi). The input string is a sequence of characters that can be interpreted as a numerical value of the specified type. 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' or L'\0') terminating the string.

The str argument to atoiand _wtoi has the following form:

[whitespace] [sign] [digits]]

A whitespace consists of space or tab characters, which are ignored; sign is either plus (+) or minus (–); and digits are one or more digits.

The versions of these functions with the _l suffix are identical except that they use the locale parameter passed in instead of the current locale. For more information, see Locale.

jianghandaxue 2012-11-02
  • 打赏
  • 举报
回复
atoi 不解释了
rxguoblp 2012-11-01
  • 打赏
  • 举报
回复
没那么麻烦,直接int result = atoi(text_value)。result 就是你想要的整形数值。
  • 打赏
  • 举报
回复
_ttoi(CString str);
如果你是想把一个CEdit编辑框控件中输入的数字直接带到程序里
我建议你把那个控件关联变量的类型选为int
这样就不用转来转去折腾了
lgh082 2012-10-31
  • 打赏
  • 举报
回复
CString str;
int i=0;
str.Format("%d",i);//将整形转换成CString类型
i=atoi(str); //将CString转换成Int类型
华美乐章 2012-10-31
  • 打赏
  • 举报
回复
atoi(text_value);
Eleven 2012-10-31
  • 打赏
  • 举报
回复
_ttoi(str);
小新小小新 2012-10-31
  • 打赏
  • 举报
回复
习惯用_ttoi,另外cstring使用时最好用
_ttoi(str.GetBuffer());
str.ReleaseBuffer();
liu7716640 2012-10-31
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
atoi
[/Quote]
可以写的详细点吗 谢谢
zerglordyzj 2012-10-31
  • 打赏
  • 举报
回复
atoi

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧