Vla*_*cic 39
如果您正在使用TCHAR.H
例程(隐式或显式),请确保使用_ttoi()
函数,以便它编译Unicode和ANSI编译.
更多细节:https://msdn.microsoft.com/en-us/library/yd5xkb5c.aspx
小智 36
最简单的方法是使用以下atoi()
函数stdlib.h
:
CString s = "123";
int x = atoi( s );
Run Code Online (Sandbox Code Playgroud)
但是,这不适用于字符串不包含有效整数的情况,在这种情况下,您应该调查strtol()
函数:
CString s = "12zzz"; // bad integer
char * p;
int x = strtol ( s, & p, 10 );
if ( * p != 0 ) {
// s does not contain an integer
}
Run Code Online (Sandbox Code Playgroud)
PaV*_*PaV 16
CString s;
int i;
i = _wtoi(s); // if you use wide charater formats
i = _atoi(s); // otherwise
Run Code Online (Sandbox Code Playgroud)
小智 9
一个_ttoi
函数可以转换CString
为整数,既宽字符和ANSI字符可以工作.以下是详细信息:
CString str = _T("123");
int i = _ttoi(str);
Run Code Online (Sandbox Code Playgroud)
你也可以使用旧的sscanf.
CString s;
int i;
int j = _stscanf(s, _T("%d"), &i);
if (j != 1)
{
// tranfer didn't work
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
126542 次 |
最近记录: |