今天在写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
本文介绍了在MFC编程中将wchar类型转换为int类型的两种有效方法:一是使用wcstol函数;二是先通过CT2A宏转换为char*类型,再使用atoi函数。两种方法各有优劣,可根据具体需求选择。
5029

被折叠的 条评论
为什么被折叠?



