今天在写MFC程序的时候遇到了将wchar转成int的问题,经过查找标准库和stackoverflow,找到了两种办法可以实现,分享给大家。
方法一:
CString strPort;
...
wchar_t* end;
//利用wcstol函数将wchar转成int
//strPort.GetBuffer(...)返回类型是wchar_t*
port = wcstol(strPort.GetBuffer(1),&end,10);
参考:http://en.cppreference.com/w/cpp/string/wide/wcstol
方法二:
CString strPort;
//利用CT2A宏将CStrin转成char*,再用atoi将char*转成int
port = atoi((CT2A)strPort);
参考:https://stackoverflow.com/questions/859304/convert-cstring-to-const-char