C++问题,CString 类型 字符 想要去掉前三个字节,把剩下的字节转化成INT型,如何实现
答案:3 悬赏:80 手机版
解决时间 2021-11-12 03:50
- 提问者网友:几叶到寒
- 2021-11-11 03:08
C++问题,CString 类型 字符 想要去掉前三个字节,把剩下的字节转化成INT型,如何实现
最佳答案
- 五星知识达人网友:人類模型
- 2021-11-11 03:21
CString ns = s.Mid(3);//Mid设置开始位置,第二参数是个数,默认时为字符串结束。
int n = _wtoi(ns);//n就是数字了
int n = _wtoi(ns);//n就是数字了
全部回答
- 1楼网友:千夜
- 2021-11-11 04:19
_wtoi(ns.Mid(3));
- 2楼网友:独钓一江月
- 2021-11-11 03:44
CString ns ;
int n=atoi( ns.c_str()+3 ) ;追问error C2039: “c_str”: 不是“ATL::CStringT”的成员追答
{
CString s = m_comnum;
CString ns = s.Mid(3);
int n = atoi( (LPCTSTR) ns );
return n;
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
CString str1("COM1");
CString str2("COM12");
cout << "n1=" << ston( str1 ) << endl ;
cout << "n2=" << ston( str2 ) << endl ;
}
return nRetCode;
}
int n=atoi( ns.c_str()+3 ) ;追问error C2039: “c_str”: 不是“ATL::CStringT
刚查了一下,还是用Mid函数吧,c_str()是C++string类的成员函数
我写了一个测试程序,你看一下
int ston( CString m_comnum ){
CString s = m_comnum;
CString ns = s.Mid(3);
int n = atoi( (LPCTSTR) ns );
return n;
}
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
CString str1("COM1");
CString str2("COM12");
cout << "n1=" << ston( str1 ) << endl ;
cout << "n2=" << ston( str2 ) << endl ;
}
return nRetCode;
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯