一般情况下,项目属性页的字符集使用多字节字符集方式。使用这种字符集设置,若将CString类型转化为int的场合,我们常用atol()函数来实现。但若使用Unicode字符集设置,以上代码就行不通了。
正确解决字符串转化为整数的方法总结如下:
1、适用于字符集为多字节字符集,而不能用于Unicode字符集
CString m_strBaudRate="9600";
int m_dwBaudRate;
m_dwBaudRate= atol((LPSTR)(LPCTSTR)m_strBaudRate);
CString strTemp;
strTemp.Format("%d",m_dwBaudRate);
MessageBox(strTemp);
2、适用于字符集为Unicode字符集,而不能用于多字节字符集
CString m_strBaudRate=L"9600";
int m_dwBaudRate;
m_dwBaudRate= _wtol(m_strBaudRate);
CString strTemp;
strTemp.Format(L"%d",m_dwBaudRate);
MessageBox(strTemp);
3、既适用于字符集为多字节字符集,又适用于Unicode字符集
CString m_strBaudRate=_T("9600");
int m_dwBaudRate;
m_dwBaudRate= _ttol(m_strBaudRate);
CString strTemp;
strTemp.Format(_T("%d"),m_dwBaudRate);
MessageBox(strTemp);
浙公网安备 33010602011771号