CString和string在unicode与非unicode下的相互转换
最近想写一个手机控制电脑的玩具,涉及到了socket通信,数据采用json通用格式,首先是jsoncpp的编译问题太烦了,然后还有更烦的,java中的String多容易的玩意儿,然后到了c/c++/mfc中超级烦,搜索了很久的攻略,用了大把的时间,最后写了个这玩意儿出来,或许可以帮助到一些需要此的道友们哈
string toString(CString cs) {
#ifdef _UNICODE
//如果是unicode工程
USES_CONVERSION;
std::string str(W2A(cs));
return str;
#else
//如果是多字节工程
std::string str(cs.GetBuffer());
cs.ReleaseBuffer();
return str;
#endif
}
CString toCString(string str) {
#ifdef _UNICODE
//如果是unicode工程
USES_CONVERSION; CString s(str.c_str());
CString ans(str.c_str());
return ans;
#else
//如果是多字节工程
CString ans;
ans.Format("%s", str.c_str());
return ans;
#endif
}
---------------------
作者:A_辉
来源:优快云
原文:https://blog.youkuaiyun.com/u014303844/article/details/51397556
版权声明:本文为博主原创文章,转载请附上博文链接!